python-trio / trio-asyncio

a re-implementation of the asyncio mainloop on top of Trio
Other
188 stars 37 forks source link

Can't get examples to work #62

Closed ronf closed 4 years ago

ronf commented 4 years ago

Sorry for such a basic question, but I've been trying for the last hour or so to get some of the examples in the documentation to run and none of the variations I've tried seem to work. I must be missing something, but I'm not sure what.

For instance, I tried the following:

import asyncio
import trio
import trio_asyncio

async def async_main():
    await asyncio.sleep(1)

async def trio_main():
    await trio_asyncio.aio_as_trio(async_main)()

trio_asyncio.run(trio_main)

and I get back:

/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio_asyncio/base.py:718: TrioDeprecationWarning: trio.Event.clear is deprecated since Trio 0.12.0; use multiple Event objects or other synchronization primitives instead (https://github.com/python-trio/trio/issues/637)
  self._stopped.clear()
Traceback (most recent call last):
  File "/tmp/test.py", line 11, in <module>
    trio_asyncio.run(trio_main)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio_asyncio/loop.py", line 393, in run
    trio.run(_run_task, proc, args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio/_core/_run.py", line 1804, in run
    raise runner.main_task_outcome.error
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio_asyncio/loop.py", line 391, in _run_task
    return await proc(*args)
  File "/tmp/test.py", line 9, in trio_main
    await trio_asyncio.aio_as_trio(async_main)()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio_asyncio/adapter.py", line 76, in __call__
    return await self.loop.run_aio_coroutine(f)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio_asyncio/base.py", line 229, in run_aio_coroutine
    return await run_aio_future(coro)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio_asyncio/util.py", line 39, in run_aio_future
    res = await trio.hazmat.wait_task_rescheduled(abort_cb)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio/_core/_traps.py", line 165, in wait_task_rescheduled
    return (await _async_yield(WaitTaskRescheduled(abort_func))).unwrap()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/outcome/_sync.py", line 111, in unwrap
    raise captured_error
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/trio_asyncio/adapter.py", line 39, in _call_defer
    return await proc(*args, **kwargs)
  File "/tmp/test.py", line 6, in async_main
    await asyncio.sleep(1)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py", line 637, in sleep
    loop = events.get_running_loop()
RuntimeError: no running event loop

I've tried other variations where I switch out the callable for an awaitable or where I use the "open_loop" call or the "run_asyncio" call shown in some of the other examples but all of them pretty much end up telling me I don't have a running event loop. What am I missing?

ronf commented 4 years ago

Following up on my own question, it appears this may be an incompatibility with Python 3.8. When I try the example under Python 3.7, it does run the sleep. However, I still get a trio deprecation error:

/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/trio_asyncio/base.py:718: TrioDeprecationWarning: trio.Event.clear is deprecated since Trio 0.12.0; use multiple Event objects or other synchronization primitives instead (https://github.com/python-trio/trio/issues/637)

pquentin commented 4 years ago

Can you please try the 0.11.0 release and tell us if it helps?

oremanj commented 4 years ago

I verified that the given example works on the latest trio-asyncio release, so I'm closing this.

ronf commented 4 years ago

Thanks - I can confirm that the example works for me as well with 0.11.0, on Python versions 3.6, 3.7, and 3.8!