fork download
  1. import asyncio
  2. import random
  3.  
  4.  
  5. @asyncio.coroutine
  6. def main():
  7. pool = asyncio.Queue()
  8. pool.put_nowait(1)
  9. pool.put_nowait(2)
  10. pool.put_nowait(3)
  11.  
  12. @asyncio.coroutine
  13. def pooled(coro):
  14. id = yield from pool.get()
  15. print('+ task')
  16. try:
  17. return (yield from coro)
  18. finally:
  19. print('- task')
  20. pool.put_nowait(id)
  21.  
  22. tasks = [
  23. pooled(asyncio.sleep(random.randint(1, 2)))
  24. for i in range(10)
  25. ]
  26.  
  27. yield from asyncio.wait(tasks)
  28.  
  29.  
  30. loop = asyncio.get_event_loop()
  31. loop.run_until_complete(main())
Success #stdin #stdout 0.13s 15688KB
stdin
Standard input is empty
stdout
+ task
+ task
+ task
- task
- task
- task
+ task
+ task
+ task
- task
+ task
- task
- task
+ task
+ task
- task
+ task
- task
- task
- task