Shu*_*kle 5 python python-3.x python-asyncio
我一直在使用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))
Run Code Online (Sandbox Code Playgroud)
wait_for_next应该等待下一个响应,但是while循环阻塞了run()函数.我怎么能阻止这种情况发生?我应该使用loop.run_in_executor,如果是这样,怎么样?
(我可以找到其他几个例子,但它们非常具体,我不明白我们的问题/解决方案是否相同.)
如前所述,循环停滞,因为await wait_for_next(response)块执行流程直到此协程无法完成.
如果你想在不阻塞执行流程的情况下启动你的一些协同程序,你可以使用函数将其作为asyncio.Task(更多关于任务)启动它ensure_future:
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())
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
4
5
6
7
8
9
10
waiting 10 10
11
12
13
14
done
15
16
17
18
19
20
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4166 次 |
| 最近记录: |