Python Asyncio - Pythonic Way Of Waiting Until Condition Satisfied
I need to wait until a certain condition/equation becomes True in a asynchronous function in python. Basically it is a flag variable which would be flagged by a coroutine running i
Solution 1:
Using of asyncio.Event
is quite straightforward. Sample below.
Note: Event should be created from inside coroutine for correct work.
import asyncio
async def bg_tsk(flag):
await asyncio.sleep(3)
flag.set()
async def waiter():
flag = asyncio.Event()
asyncio.create_task(bg_tsk(flag))
await flag.wait()
print("After waiting")
asyncio.run(waiter())
Post a Comment for "Python Asyncio - Pythonic Way Of Waiting Until Condition Satisfied"