小编cyb*_*oul的帖子

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:

import asyncio
flag = False

async def bg_tsk():
    await asyncio.sleep(10)
    flag = True

async def waiter():
    asyncio.create_task(bg_tsk())
    await asyncio.sleep(0)
    # I find the below part unpythonic
    while not flag:
        await …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio

8
推荐指数
1
解决办法
5935
查看次数

标签 统计

python ×1

python-3.x ×1

python-asyncio ×1