在 asyncio 中等待 while 循环
await while loop in asyncio
如何等待while循环?我有以下代码片段:
import asyncio
from asyncio.tasks import sleep
import time
running = True
async def f1():
global running
print(f"Running: {running}")
print("f1() started.")
### await the while loop below
while running:
print(f"Inside while loop")
###
print(f"Running: {running}")
print("f1() ended")
async def f2():
global running
print("f2() started.")
# this should be blocking code
time.sleep(0.5)
print("f2 ended.")
running = False
async def main():
await asyncio.gather(f1(), f2())
asyncio.run(main())
基本上我想要的是:
# 1. Start f1(). f1() has a while loop.
# 2. Start f2().
# 3. Notify f1() to break the while loop.
我猜你必须在 while loop
中加入一些 await asyncio.sleep(0)
左右的内容,不确定它是否适用于 0
但只需要输入一个小数字。它只需要一些等待调用,让其他人有机会做
import asyncio
from asyncio.tasks import sleep
import time
running = True
async def f1():
global running
print(f"Running: {running}")
print("f1() started.")
### await the while loop below
while running:
print(f"Inside while loop")
await asyncio.sleep(0)
###
print(f"Running: {running}")
print("f1() ended")
async def f2():
global running
print("f2() started.")
# this should be blocking code
time.sleep(0.5)
print("f2 ended.")
running = False
async def main():
await asyncio.gather(f1(), f2())
asyncio.run(main())
适合我
没有全局变量:
import asyncio
from asyncio.tasks import sleep
import time
async def f1( q ):
print("f1() started.")
### await the while loop below
## Start listener
event = asyncio.ensure_future( q.get() )
while True:
print( "Inside While Loop" )
if event.done():
break
else:
await asyncio.sleep( 0.01 )
###
print("f1() ended")
async def f2( q ):
print("f2() started.")
# this should be blocking code
time.sleep( 3 )
print("f2 ended.")
await q.put( 1 )
async def main():
q = asyncio.Queue( 1 )
await asyncio.gather(f1( q ), f2( q ))
asyncio.run(main())
但是,由于 f2 正在阻塞,它在被阻塞时实际上不会打印“内部事件循环”
所以你会得到类似
的输出
f1() started.
Inside While Loop
f2() started.
f2 ended.
Inside While Loop
f1() ended
此外,您几乎可以在该队列中放入任何东西,不必 1
。
如何等待while循环?我有以下代码片段:
import asyncio
from asyncio.tasks import sleep
import time
running = True
async def f1():
global running
print(f"Running: {running}")
print("f1() started.")
### await the while loop below
while running:
print(f"Inside while loop")
###
print(f"Running: {running}")
print("f1() ended")
async def f2():
global running
print("f2() started.")
# this should be blocking code
time.sleep(0.5)
print("f2 ended.")
running = False
async def main():
await asyncio.gather(f1(), f2())
asyncio.run(main())
基本上我想要的是:
# 1. Start f1(). f1() has a while loop.
# 2. Start f2().
# 3. Notify f1() to break the while loop.
我猜你必须在 while loop
中加入一些 await asyncio.sleep(0)
左右的内容,不确定它是否适用于 0
但只需要输入一个小数字。它只需要一些等待调用,让其他人有机会做
import asyncio
from asyncio.tasks import sleep
import time
running = True
async def f1():
global running
print(f"Running: {running}")
print("f1() started.")
### await the while loop below
while running:
print(f"Inside while loop")
await asyncio.sleep(0)
###
print(f"Running: {running}")
print("f1() ended")
async def f2():
global running
print("f2() started.")
# this should be blocking code
time.sleep(0.5)
print("f2 ended.")
running = False
async def main():
await asyncio.gather(f1(), f2())
asyncio.run(main())
适合我
没有全局变量:
import asyncio
from asyncio.tasks import sleep
import time
async def f1( q ):
print("f1() started.")
### await the while loop below
## Start listener
event = asyncio.ensure_future( q.get() )
while True:
print( "Inside While Loop" )
if event.done():
break
else:
await asyncio.sleep( 0.01 )
###
print("f1() ended")
async def f2( q ):
print("f2() started.")
# this should be blocking code
time.sleep( 3 )
print("f2 ended.")
await q.put( 1 )
async def main():
q = asyncio.Queue( 1 )
await asyncio.gather(f1( q ), f2( q ))
asyncio.run(main())
但是,由于 f2 正在阻塞,它在被阻塞时实际上不会打印“内部事件循环”
所以你会得到类似
的输出f1() started.
Inside While Loop
f2() started.
f2 ended.
Inside While Loop
f1() ended
此外,您几乎可以在该队列中放入任何东西,不必 1
。