fork(1) download
  1. import asyncio
  2. import random
  3.  
  4.  
  5. @asyncio.coroutine
  6. def main():
  7. pool = asyncio.Semaphore(3)
  8.  
  9. @asyncio.coroutine
  10. def pooled(coro):
  11. with (yield from pool):
  12. print("+ task")
  13. try:
  14. return (yield from coro)
  15. finally:
  16. print("- task")
  17.  
  18. tasks = [
  19. pooled(asyncio.sleep(random.randint(1, 2)))
  20. for i in range(10)
  21. ]
  22.  
  23. yield from asyncio.wait(tasks)
  24.  
  25.  
  26. loop = asyncio.get_event_loop()
  27. loop.run_until_complete(main())
Success #stdin #stdout 0.14s 15728KB
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