我想从一个使用“标准异步”的大型项目中使用一个很棒的库。库调用的某些功能curio.spawn
在从“标准异步”调用时会导致错误。有没有办法让它工作?
重现错误的示例代码:
import curio
import asyncio
async def curio_method():
await curio.sleep(1)
async def asyncio_method():
task = await curio.spawn(curio_method())
await task
asyncio.run(asyncio_method())
结果:
Traceback (most recent call last):
File "/tmp/curio_test.py", line 12, in <module>
asyncio.run(asyncio_method())
File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/tmp/curio_test.py", line 8, in asyncio_method
task = await curio.spawn(curio_method())
File "/home/garyvdm/dev/image_builder/bpmagent/ve/lib/python3.9/site-packages/curio/task.py", line 613, in spawn
task = await _spawn(coro)
File "/home/garyvdm/dev/image_builder/bpmagent/ve/lib/python3.9/site-packages/curio/traps.py", line 83, in _spawn
return await _kernel_trap('trap_spawn', coro)
File "/home/garyvdm/dev/image_builder/bpmagent/ve/lib/python3.9/site-packages/curio/traps.py", line 32, in _kernel_trap
result = yield request
RuntimeError: Task got bad yield: ('trap_spawn', <coroutine object curio_method at 0x7fc1b62eeec0>)
sys:1: RuntimeWarning: coroutine 'curio_method' was never awaited