fork download
  1. import asyncio
  2.  
  3. NUM_WORKERS = 3
  4.  
  5.  
  6. async def ask(q):
  7. while True:
  8. res = input('Enter data: ')
  9. await q.put(res)
  10. await asyncio.sleep(0.1)
  11.  
  12.  
  13. async def produce(q):
  14. while True:
  15. elem = await q.get() # ждем элемент
  16. print(f'Processing {elem} ...')
  17. await asyncio.sleep(3) # симуляция асинхронной обработки
  18. print(f'{elem} done')
  19.  
  20.  
  21. async def main():
  22. queue = asyncio.Queue()
  23. print('Main started')
  24. for _ in range(NUM_WORKERS): # запускаем сразу несколько асинхронных воркеров
  25. asyncio.create_task(produce(queue))
  26. await ask(queue)
  27.  
  28.  
  29. if __name__ == '__main__':
  30. asyncio.run(main())
  31.  
Runtime error #stdin #stdout #stderr 0.23s 25564KB
stdin
Standard input is empty
stdout
Main started
Enter data: 
stderr
Traceback (most recent call last):
  File "./prog.py", line 30, in <module>
  File "/usr/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "./prog.py", line 26, in main
  File "./prog.py", line 8, in ask
EOFError: EOF when reading a line