Created
June 25, 2020 10:34
-
-
Save velotiotech/1477131948ca23879167df0281726d02 to your computer and use it in GitHub Desktop.
consumer_producer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example 1 | |
from tornado import gen, queues | |
from tornado.ioloop import IOLoop | |
@gen.coroutine | |
def consumer(queue, num_expected): | |
for _ in range(num_expected): | |
# heavy I/O or network task | |
print('got: %s' % (yield queue.get())) | |
@gen.coroutine | |
def producer(queue, num_items): | |
for i in range(num_items): | |
print('putting %s' % i) | |
yield queue.put(i) | |
@gen.coroutine | |
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() | |
@gen.coroutine | |
def waiter(): | |
print("I'll wait right here") | |
yield my_condition.wait() | |
print("Received notification now doing my things") | |
@gen.coroutine | |
def notifier(): | |
yield gen.sleep(60) | |
print("About to notify") | |
my_condition.notify() | |
print("Done notifying") | |
@gen.coroutine | |
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