Skip to content

Nesting event loops #6

@davidbrochart

Description

@davidbrochart

I was intrigued by this statement: "can nest tinyio loops inside each other, none of this one-per-thread business". In asyncio nesting event loops has always been an issue, which led to e.g. nest_asyncio. But this code:

import tinyio

def bar():
    yield tinyio.sleep(1)
    print("done")

def foo():
    loop = tinyio.Loop()
    loop.run(bar())

loop = tinyio.Loop()
loop.run(foo())

gives the following error:

/home/david/git/tinyio/foo.py:12: RuntimeWarning: Coroutine `None` did not respond properly to cancellation on receiving a `tinyio.CancelledError`, and so a resource leak may have occurred. The coroutine is expected to propagate the `tinyio.CancelledError` to indicate success in cleaning up resources. Instead, the coroutine raised the exception `AttributeError: 'NoneType' object has no attribute 'throw'`.

  loop.run(foo())
AttributeError: 'NoneType' object has no attribute 'send'

During handling of the above exception, another exception occurred:

  + Exception Group Traceback (most recent call last):
  |   File "/home/david/git/tinyio/foo.py", line 12, in <module>
  |     loop.run(foo())
  |     ~~~~~~~~^^^^^^^
  |   File "/home/david/git/tinyio/tinyio/_core.py", line 68, in run
  |     _cleanup(e, waiting_on, current_coro_ref, exception_group)
  |     ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/david/git/tinyio/tinyio/_core.py", line 281, in _cleanup
  |     raise BaseExceptionGroup(
  |     ...<7 lines>...
  |     )
  | ExceptionGroup: An error occured running a `tinyio` loop.
  | The first exception below is the original error. Since it is common for each coroutine to only have one other coroutine waiting on it, then we have stitched together their tracebacks for as long as that is possible.
  | The other exceptions are all exceptions that occurred whilst stopping the other coroutines.
  | (For a debugger that allows for navigating within exception groups, try `https://github.com/patrick-kidger/patdb`.)
  |  (2 sub-exceptions)
  +-+---------------- 1 ----------------
    | AttributeError: 'NoneType' object has no attribute 'send'
    +---------------- 2 ----------------
    | AttributeError: 'NoneType' object has no attribute 'throw'
    +------------------------------------

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions