examples: add Select lifetimes to example.

issue510
David Wilson 6 years ago
parent 5dc916af5e
commit d8ac50bf01

@ -66,16 +66,23 @@ def main(router):
# Select will wake it up -- be it a message arriving on the status
# receiver, or any message arriving on any of the function call result
# receivers.
both_sel = mitogen.select.Select([status_recv, calls_sel], oneshot=False)
# Once last call is completed, calls_sel will be empty since it's
# oneshot=True (the default), causing __bool__ to be False
both_sel = mitogen.select.Select([status_recv, calls_sel], oneshot=False)
# Internally selects store a strong reference from Receiver->Select that
# will keep the Select alive as long as the receiver is alive. If a
# receiver or select otherwise 'outlives' some parent select, attempting to
# re-add it to a new select will raise an error. In all cases it's
# desirable to call Select.close(). This can be done as a context manager.
with calls_sel, both_sel:
while calls_sel:
try:
msg = both_sel.get(timeout=60.0)
except mitogen.core.TimeoutError:
print("No update in 60 seconds, something's broke")
raise Crash()
break
hostname = hostname_by_context_id[msg.src_id]
@ -90,4 +97,7 @@ def main(router):
except mitogen.core.CallError as e:
print('Task failed on host %s: %s' % (hostname, e))
if calls_sel:
print('Some tasks did not complete.')
else:
print('All tasks completed.')

Loading…
Cancel
Save