Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot run asyncio.sleep in a greenlet #16

Open
sillydan1 opened this issue Jan 26, 2024 · 0 comments
Open

Cannot run asyncio.sleep in a greenlet #16

sillydan1 opened this issue Jan 26, 2024 · 0 comments

Comments

@sillydan1
Copy link

sillydan1 commented Jan 26, 2024

I want to run an asyncio-based function inside a greenlet but I am running into an issue where the eventloop is started whilst another is already running. Here's a minimal example:

import gevent.monkey
gevent.monkey.patch_all()
import asyncio
import asyncio_gevent

asyncio.set_event_loop_policy(asyncio_gevent.EventLoopPolicy())

import gevent

@asyncio_gevent.async_to_sync
async def async_main():
    await asyncio.sleep(1)


def main():
    gevent.joinall([
        gevent.spawn(async_main),
        gevent.spawn(async_main),
    ])

if __name__ == "__main__":
    main()

This will fail with the following output:

Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 596, in run_forever
    self._check_running()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 590, in _check_running
    raise RuntimeError(
RuntimeError: Cannot run the event loop while another loop is running
2024-01-26T10:07:42Z <Greenlet at 0x7fdd85be36a0: <bound method BaseEventLoop.run_forever of <EventLoop running=False closed=False debug=False>>> failed with RuntimeError

Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 651, in run_until_complete
    raise RuntimeError('Event loop stopped before Future completed.')
RuntimeError: Event loop stopped before Future completed.
2024-01-26T10:07:42Z <Greenlet at 0x7fdd85be3560: <bound method BaseEventLoop.run_until_complete of <EventLoop running=False closed=False debug=False>>(<Task pending name='Task-2' coro=<async_main() run)> failed with RuntimeError

Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/usr/local/lib/python3.11/site-packages/asyncio_gevent/future_to_greenlet.py", line 118, in _run
    return ensured_future.result()
           ^^^^^^^^^^^^^^^^^^^^^^^
asyncio.exceptions.InvalidStateError: Result is not set.
2024-01-26T10:07:42Z <Greenlet at 0x7fdd85be2de0: _run(future=<coroutine object async_main at 0x7fdd85d82980>, loop=None, autostart_future=True, on_cancelled=<function future_to_greenlet.<locals>.on_cancelled)> failed with InvalidStateError

Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/usr/local/lib/python3.11/site-packages/asyncio_gevent/async_to_sync.py", line 37, in fn
    return greenlet.get()
           ^^^^^^^^^^^^^^
  File "src/gevent/greenlet.py", line 805, in gevent._gevent_cgreenlet.Greenlet.get
  File "src/gevent/greenlet.py", line 373, in gevent._gevent_cgreenlet.Greenlet._raise_exception
  File "/usr/local/lib/python3.11/site-packages/gevent/_compat.py", line 66, in reraise
    raise value.with_traceback(tb)
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/usr/local/lib/python3.11/site-packages/asyncio_gevent/future_to_greenlet.py", line 118, in _run
    return ensured_future.result()
    ^^^^^^^^^^^^^^^^^
asyncio.exceptions.InvalidStateError: Result is not set.
2024-01-26T10:07:42Z <Greenlet at 0x7fdd85be2ac0: fn> failed with InvalidStateError

sys:1: RuntimeWarning: coroutine 'async_main' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<async_main() running at /code/src/comsdk/cli/main.py:10>>

I suspect that I am using either asyncio, gevent or asyncio-gevent wrongly here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant