![](/img/trans.png)
[英]intercepting the call to __await__ on an asyncio Task in Python
[英]Python asyncio await-for timeout doesn't work for infinite task
在 Python 3.8 中,我想我明白為什么除非我包含asyncio.sleep(1)
否則下面的forever
函數也不會在 1 秒后超時。 這是因為它在離開隊列后開始執行並且隊列中沒有其他任務運行,因為它從不阻塞?
我需要完成的是調用用戶提供的一些函數(如永遠)。 他們可能會給我一個永遠不會返回的函數(例如它無限循環並且不會阻塞)。 那么如何獲得我想要的調用用戶函數並允許它運行直到超時的效果呢?
import asyncio
async def long_time():
# Sleep for one hour
await asyncio.sleep(3600)
print('yay!')
async def forever():
while True:
pass
#await asyncio.sleep(1)
async def main():
# Wait for at most 1 second
try:
await asyncio.wait_for(long_time(), timeout=1.0)
except asyncio.TimeoutError:
print('timeout 1!')
try:
await asyncio.wait_for(forever(), timeout=1.0)
except asyncio.TimeoutError:
print('timeout 2!')
asyncio.run(main())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.