Improve async replication example.

This commit is contained in:
Oleksandr Shulgin 2015-10-23 17:51:03 +02:00
parent e69dafbecc
commit dd6bcbd04f

View File

@ -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