如何使用异步函数修复此错误 sys:1: RuntimeWarning: coroutine 'finish_process' was never awaited
how do I fix this error with async functions `sys:1: RuntimeWarning: coroutine 'finish_process' was never awaited`
这段代码的目标是让它运行成为主函数,调用main_process()
,直到结束的异步函数,调用finish_process()
,设置变量finish_state
到 True
并且循环不会重复。
import asyncio
import time
condition = True
finish_state = False
x = 0
async def finish_process(finish_state):
finish_state = True
time.sleep(5)
return finish_state
async def main_process(condition,finish_state,x):
while condition == True:
finish_state = await asyncio.run(finish_process(finish_state))
x = x + 1
print(x)
#if(x > 10):
if(finish_state == True):
print("Termina!")
condition = False
asyncio.run(main_process(condition,finish_state,x))
我已经将异步函数调用的等待放在另一个异步函数中,我不明白为什么它一直报错await
。
我认为用 await
或旧的 yield from
指示应该可以解决同时等待其他函数结果的问题。
raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'finish_process' was never awaited
您的程序存在以下问题:
- 你应该使用
asyncio.create_task
or asyncio.gather
to run asynchronous tasks, not asyncio.run
.
- 可以用
await asyncio.sleep(5)
代替time.sleep(5)
import asyncio
import time
condition = True
finish_state = False
x = 0
async def finish_process(finish_state):
finish_state = True
await asyncio.sleep(5)
return finish_state
async def main_process(condition,finish_state,x):
while condition == True:
finish_state = await asyncio.create_task(finish_process(finish_state))
x = x + 1
print(x)
#if(x > 10):
if(finish_state == True):
print("Termina!")
condition = False
asyncio.run(main_process(condition,finish_state,x))
输出:
1
Termina!
这段代码的目标是让它运行成为主函数,调用main_process()
,直到结束的异步函数,调用finish_process()
,设置变量finish_state
到 True
并且循环不会重复。
import asyncio
import time
condition = True
finish_state = False
x = 0
async def finish_process(finish_state):
finish_state = True
time.sleep(5)
return finish_state
async def main_process(condition,finish_state,x):
while condition == True:
finish_state = await asyncio.run(finish_process(finish_state))
x = x + 1
print(x)
#if(x > 10):
if(finish_state == True):
print("Termina!")
condition = False
asyncio.run(main_process(condition,finish_state,x))
我已经将异步函数调用的等待放在另一个异步函数中,我不明白为什么它一直报错await
。
我认为用 await
或旧的 yield from
指示应该可以解决同时等待其他函数结果的问题。
raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'finish_process' was never awaited
您的程序存在以下问题:
- 你应该使用
asyncio.create_task
orasyncio.gather
to run asynchronous tasks, notasyncio.run
. - 可以用
await asyncio.sleep(5)
代替
time.sleep(5)
import asyncio
import time
condition = True
finish_state = False
x = 0
async def finish_process(finish_state):
finish_state = True
await asyncio.sleep(5)
return finish_state
async def main_process(condition,finish_state,x):
while condition == True:
finish_state = await asyncio.create_task(finish_process(finish_state))
x = x + 1
print(x)
#if(x > 10):
if(finish_state == True):
print("Termina!")
condition = False
asyncio.run(main_process(condition,finish_state,x))
输出:
1
Termina!