mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-07-03 19:23:03 +03:00
Improve async replication example.
This commit is contained in:
parent
e69dafbecc
commit
dd6bcbd04f
|
@ -560,6 +560,9 @@ The individual messages in the replication stream are represented by
|
||||||
|
|
||||||
An actual example of asynchronous operation might look like this::
|
An actual example of asynchronous operation might look like this::
|
||||||
|
|
||||||
|
from select import select
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
def consume(msg):
|
def consume(msg):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
@ -571,14 +574,12 @@ The individual messages in the replication stream are represented by
|
||||||
else:
|
else:
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
timeout = keepalive_interval - (now - cur.io_timestamp).total_seconds()
|
timeout = keepalive_interval - (now - cur.io_timestamp).total_seconds()
|
||||||
if timeout > 0:
|
try:
|
||||||
sel = select.select([cur], [], [], timeout)
|
sel = select([cur], [], [], max(0, timeout))
|
||||||
else:
|
if not any(sel):
|
||||||
sel = ([], [], [])
|
cur.send_feedback() # timed out, send keepalive message
|
||||||
|
except InterruptedError:
|
||||||
if not sel[0]:
|
pass # recalculate timeout and continue
|
||||||
# timed out, send keepalive message
|
|
||||||
cur.send_feedback()
|
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
pair: Cursor; Replication
|
pair: Cursor; Replication
|
||||||
|
|
Loading…
Reference in New Issue
Block a user