Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. from tornado import gen
  2. from tornado.ioloop import IOLoop
  3. from tornado.queues import Queue
  4.  
  5. q = Queue(maxsize=2)
  6.  
  7. @gen.coroutine
  8. def consumer():
  9. while True:
  10. item = yield q.get()
  11. try:
  12. print('Doing work on %s' % item)
  13. yield gen.sleep(0.01)
  14. finally:
  15. q.task_done()
  16.  
  17. @gen.coroutine
  18. def producer():
  19. for item in range(5):
  20. yield q.put(item)
  21. print('Put %s' % item)
  22.  
  23. @gen.coroutine
  24. def main():
  25. # Start consumer without waiting (since it never finishes).
  26. IOLoop.current().spawn_callback(consumer)
  27. yield producer() # Wait for producer to put all tasks.
  28. yield q.join() # Wait for consumer to finish all tasks.
  29. print('Done')
  30.  
  31. IOLoop.current().run_sync(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement