0

There is a great library that I want to use from a larger project that I'm working on that uses "standard asyncio". Some of the functionality of the library calls curio.spawn which results in an error when called from "standard asyncio". Is there a way to get this to work?

Example code that reproduces the error:

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())

Result:

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
Gary van der Merwe
  • 9,134
  • 3
  • 49
  • 80

1 Answers1

0

Instead of asyncio.run, you can use curio.run.

But, if you need to use asyncio.run, you can put your curio.run inside your asyncio.run logic. Anyway, I think that is not the idea.

Lucas Vazquez
  • 1,456
  • 16
  • 20