I'm stuck here a little bit with surely a simple thing. Briefly about the problem:
- connected asyncio
- Created 2 async user_io and task_manager cortices
- In user_io () there is an input that is completely ignored by await
- task_manager () is executed only after the completion of user_io ()
Attention question - How to make waiting for input and other korutiny "parallel"?
Thanks for the help!
# ΠΠ΄ΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΈΠΉ Π²Π²ΠΎΠ΄ async def user_io(): try: # ΠΠ΄ΡΠΌ Π΄Π΅ΠΉΡΡΠ²ΠΈΡ ΠΎΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ while True: # Await, ΠΊΠΎΡΠΎΡΡΠΉ Π½ΠΈΠΆΠ΅ Π½Π΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ! command = await input('ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ (Π‘ to EXIT): ') if re.match(r'[cC]', command): break except KeyboardInterrupt: print('Π‘ΠΊΡΠΈΠΏΡ ΠΎΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½ ΠΏΠΎ ΠΊΠΎΠΌΠ±ΠΈΠ½Π°ΡΠΈΠΈ ΠΊΠ»Π°Π²ΠΈΡ') # ΠΠΎΡΡΡΠΈΠ½Π°, Π²ΡΠΏΠΎΠ»Π½ΡΠ΅ΠΌΠ°Ρ ΠΏΠ°ΡΠ°Π»Π»Π΅Π»ΡΠ½ΠΎ Ρ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ΠΌ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΎΠ³ΠΎ Π²Π²ΠΎΠ΄Π° async def task_manager(): await asyncio.sleep(1) print("I'm a task manager 1!") await asyncio.sleep(1) print("I'm a task manager 2!") if __name__=="__main__": # ΠΠ°ΠΏΡΡΠΊΠ°Π΅ΠΌ ΡΠΈΠΊΠ» ΡΠΎΠ±ΡΡΠΈΠΉ loop = asyncio.get_event_loop() tasks = [ loop.create_task(user_io()), loop.create_task(task_manager()) ] loop.run_until_complete(asyncio.wait(tasks)) loop.close() sys.exit()