Skip to content

Instantly share code, notes, and snippets.

@dw

dw/unidirectional.py

Last active Mar 26, 2018
Embed
What would you like to do?
import mitogen
import time
import traceback
import os
import mitogen.core
@mitogen.core.takes_router
def maker(context_id, handle, router):
context = mitogen.core.Context(router, context_id)
sender = mitogen.core.Sender(context, handle)
# Sleep for 1 sec then generate some result.
for x in range(4):
sender.put(x)
time.sleep(1)
sender.close()
return 1234
@mitogen.main(log_level='DEBUG')
def main(router):
router.enable_debug()
target = router.local()
receiver = mitogen.core.Receiver(router)
resp = target.call_async(maker, mitogen.context_id, receiver.handle)
try:
# Show each int as it's produced.
for msg in receiver:
print msg.unpickle()
print 'and we are done'
# Show return value.
print 'RETURN VALUE:', resp.get().unpickle()
except:
traceback.print_exc()
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment