gpt4 book ai didi

python - 将协程传递给 AbstractEventLoop.call_later

转载 作者:行者123 更新时间:2023-12-01 02:17:42 25 4
gpt4 key购买 nike

以下是我尝试运行的代码:

>>> import asyncio
>>> async def foo(loop, iv):
... await asyncio.sleep(1, loop=loop)
... print(f'done: {iv}')
...
>>> loop = asyncio.get_event_loop()
>>> loop.call_later(2, foo, loop, 10)
<TimerHandle when=36395.554089349 foo(<_UnixSelecto...e debug=False>, 10) at <input>:1>
>>> loop.run_forever()

(Python 3.6)

基本上,foo() 函数有一些链式async 调用,因此此方法必须是async,因为需要await 进行链式调用。然而该方法是在延迟后触发的,当运行该代码时,会出现以下问题:

/usr/lib64/python3.6/asyncio/events.py:127: RuntimeWarning: coroutine 'foo' was never awaited self._callback(*self._args)

call_later 中处理此 async 调用的正确方法是什么?

最佳答案

call_later() 仅支持回调(常规函数);你根本无法传入协程。

如果你想延迟一个协程,你有两个选择;要么通过让协程在开始时休眠来延迟协程,要么调用 asyncio.create_task()来自 call_later(),它确实采用协程并安排其运行。

在例程开始时使用asyncio.sleep(),您可以让循环直接执行它:

async def foo(iv):
# delay start of the work
await asyncio.sleep(2)

# rest of your coroutine

您可以轻松地使用包装协程来执行此操作:

async def await_coro_later(delay, coro, *args, **kwargs):
await asyncio.sleep(delay)
await coro(*args, **kwargs)

如果您使用 asyncio.create_task() (或者,对于 Python 3.6 或更早版本, asyncio.ensure_future() ,您可以将传递给 call_later():

# create a task for foo(10) later
loop.call_later(2, asyncio.create_task, foo(10))

任一技术的演示:

>>> import asyncio
>>> async def foo(iv, start):
... await asyncio.sleep(1)
... offset = asyncio.get_running_loop().time() - start
... print(f'done ({offset:.3f}s): {iv}')
...
>>> async def await_coro_later(delay, coro, *args, **kwargs):
... await asyncio.sleep(delay)
... await coro(*args, **kwargs)
...
>>> async def demo():
... loop = asyncio.get_running_loop()
... start = loop.time()
... loop.call_later(2, asyncio.create_task, foo('cb_to_create_task', start))
... await await_coro_later(5, foo, 'coro_later', start)
...
>>> asyncio.run(demo())
done (3.004s): cb_to_create_task
done (6.006s): coro_later

关于python - 将协程传递给 AbstractEventLoop.call_later,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48235690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com