python 制作一个不等待完成的异步定时器
python Make an async timer without waiting to finish
我想制作一个在普通函数中启动的计时器,但在计时器函数中,它应该能够调用异步函数
我想做这样的事情:
startTimer()
while True:
print("e")
def startTimer(waitForSeconds: int):
# Wait for `waitForSeconds`
await myAsyncFunc()
async def myAsyncFunc():
print("in my async func")
while True 循环应该做它的事情,在 waitForSeconds 定时器之后,异步函数应该执行另一个异步函数,但是等待不应该阻止任何其他操作并且不需要等待
如果有什么不明白的地方,对不起,我会尽力解释的
谢谢
我很确定您对并发处理很熟悉,但是您没有准确地展示您想要的内容。因此,如果我对您的理解正确,您希望有 2 个进程。第一个只做 while True
,第二个进程是计时器(等待 5 秒),它将调用异步任务。我假设您根据标签使用 asyncio
:
import asyncio
async def myAsyncFunc():
print("in my async func")
async def call_after(delay):
await asyncio.sleep(delay)
await myAsyncFunc()
async def while_true():
while True:
await asyncio.sleep(1) # sleep here to avoid to large output
print("e")
async def main():
task1 = asyncio.create_task(
while_true())
task2 = asyncio.create_task(
call_after(5))
# Wait until both tasks are completed (should take
# around 2 seconds.)
await task1
await task2
asyncio.run(main())
如果您想 运行 并行处理同步和异步代码,则需要 运行 在单独的线程中处理其中之一。例如:
def sync_code():
while True:
print("e")
async def start_timer(secs):
await asyncio.sleep(secs)
await async_func()
async def main():
asyncio.create_task(start_timer(1))
loop = asyncio.get_event_loop()
# use run_in_executor to run sync code in a separate thread
# while this thread runs the event loop
await loop.run_in_executor(None, sync_code)
asyncio.run(main())
如果您不能接受以上内容(例如,因为它将整个程序变成了异步程序),您还可以 运行 后台线程中的事件循环,并使用 asyncio.run_coroutine_threadsafe
。这种方法将允许 startTimer
拥有您想要的签名(和界面):
def startTimer(waitForSeconds):
loop = asyncio.new_event_loop()
threading.Thread(daemon=True, target=loop.run_forever).start()
async def sleep_and_run():
await asyncio.sleep(waitForSeconds)
await myAsyncFunc()
asyncio.run_coroutine_threadsafe(sleep_and_run(), loop)
async def myAsyncFunc():
print("in my async func")
startTimer(1)
while True:
print("e")
我想制作一个在普通函数中启动的计时器,但在计时器函数中,它应该能够调用异步函数
我想做这样的事情:
startTimer()
while True:
print("e")
def startTimer(waitForSeconds: int):
# Wait for `waitForSeconds`
await myAsyncFunc()
async def myAsyncFunc():
print("in my async func")
while True 循环应该做它的事情,在 waitForSeconds 定时器之后,异步函数应该执行另一个异步函数,但是等待不应该阻止任何其他操作并且不需要等待
如果有什么不明白的地方,对不起,我会尽力解释的
谢谢
我很确定您对并发处理很熟悉,但是您没有准确地展示您想要的内容。因此,如果我对您的理解正确,您希望有 2 个进程。第一个只做 while True
,第二个进程是计时器(等待 5 秒),它将调用异步任务。我假设您根据标签使用 asyncio
:
import asyncio
async def myAsyncFunc():
print("in my async func")
async def call_after(delay):
await asyncio.sleep(delay)
await myAsyncFunc()
async def while_true():
while True:
await asyncio.sleep(1) # sleep here to avoid to large output
print("e")
async def main():
task1 = asyncio.create_task(
while_true())
task2 = asyncio.create_task(
call_after(5))
# Wait until both tasks are completed (should take
# around 2 seconds.)
await task1
await task2
asyncio.run(main())
如果您想 运行 并行处理同步和异步代码,则需要 运行 在单独的线程中处理其中之一。例如:
def sync_code():
while True:
print("e")
async def start_timer(secs):
await asyncio.sleep(secs)
await async_func()
async def main():
asyncio.create_task(start_timer(1))
loop = asyncio.get_event_loop()
# use run_in_executor to run sync code in a separate thread
# while this thread runs the event loop
await loop.run_in_executor(None, sync_code)
asyncio.run(main())
如果您不能接受以上内容(例如,因为它将整个程序变成了异步程序),您还可以 运行 后台线程中的事件循环,并使用 asyncio.run_coroutine_threadsafe
。这种方法将允许 startTimer
拥有您想要的签名(和界面):
def startTimer(waitForSeconds):
loop = asyncio.new_event_loop()
threading.Thread(daemon=True, target=loop.run_forever).start()
async def sleep_and_run():
await asyncio.sleep(waitForSeconds)
await myAsyncFunc()
asyncio.run_coroutine_threadsafe(sleep_and_run(), loop)
async def myAsyncFunc():
print("in my async func")
startTimer(1)
while True:
print("e")