我一直在使用asyncio,但我还是不太熟悉它。我当前的问题是,在尝试等待来自具有asyncio的函数的响应时,等待(while循环)阻止函数发生。以下是总结问题的代码:
import asyncio
response = 0
async def handle(x):
await asyncio.sleep(0.1)
return x
async def run():
global response
for number in range(1, 21):
response = await handle(number)
print(response)
if response == 10:
await wait_for_next(response)
async def wait_for_next(x):
while response == x:
print('waiting',response,x)
await asyncio.sleep(0.5)
print('done')
tasks = [run()]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
wait_for_next应该等待下一个响应,但是while循环阻塞了run()函数。我怎么能阻止这种情况发生?我应该使用loop.run_in_executor,如果是,那该怎么办?
(我可以找到其他几个例子,但它们非常具体,我不明白我们的问题/解决方案是否相同。)
答案 0 :(得分:5)
如前所述,循环卡住,因为await wait_for_next(response)
会阻止执行流程,直到此协程无法完成。
如果您希望在不阻止执行流程的情况下启动某些协程,可以使用ensure_future
函数以asyncio.Task(more关于任务)启动它:
import asyncio
response = 0
async def handle(x):
await asyncio.sleep(0.1)
return x
async def run():
global response
for number in range(1, 21):
response = await handle(number)
print(response)
if response == 10:
# run wait_for_next "in background" instead of blocking flow:
asyncio.ensure_future(wait_for_next(response))
async def wait_for_next(x):
while response == x:
print('waiting',response,x)
await asyncio.sleep(0.5)
print('done')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
输出:
1
2
3
4
5
6
7
8
9
10
waiting 10 10
11
12
13
14
done
15
16
17
18
19
20