Skip to content
Advertisement

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 in asyncio.create_task(). I want to await until it is flagged in the main loop of asyncio. Here’s my current code:

JavaScript

Is there any better implementation of this? Or is this the best way possible? I have tried using ‘asyncio.Event’, but it doesnt seem to work with ‘asyncio.create_task()’.

Advertisement

Answer

Using of asyncio.Event is quite straightforward. Sample below.

Note: Event should be created from inside coroutine for correct work.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement