Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# Example 1
from tornado import gen, queues
from tornado.ioloop import IOLoop
def consumer(queue, num_expected):
for _ in range(num_expected):
# heavy I/O or network task
print('got: %s' % (yield queue.get()))
def producer(queue, num_items):
for i in range(num_items):
print('putting %s' % i)
yield queue.put(i)
def main():
Starts producer and consumer and wait till they finish
yield [producer(q, producer_num_items), consumer(q, producer_num_items)]
queue_size = 1
producer_num_items = 5
q = queues.Queue(queue_size)
results = IOLoop.current().run_sync(main)
# Output:
# putting 0
# putting 1
# got: 0
# got: 1
# putting 2
# putting 3
# putting 4
# got: 2
# got: 3
# got: 4
# Example 2
# Condition
# A condition allows one or more coroutines to wait until notified.
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.locks import Condition
my_condition = Condition()
def waiter():
print("I'll wait right here")
yield my_condition.wait()
print("Received notification now doing my things")
def notifier():
yield gen.sleep(60)
print("About to notify")
print("Done notifying")
def runner():
# Wait for waiter() and notifier() in parallel
yield([waiter(), notifier()])
results = IOLoop.current().run_sync(runner)
# output:
# I'll wait right here
# About to notify
# Done notifying
# Received notification now doing my things
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment