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

When using multiple event loops, asyncio will raise an error. #12

Open
clanet opened this issue Apr 13, 2023 · 0 comments
Open

When using multiple event loops, asyncio will raise an error. #12

clanet opened this issue Apr 13, 2023 · 0 comments

Comments

@clanet
Copy link

clanet commented Apr 13, 2023

When running the following program, you will get a 'RuntimeError: Cannot run the event loop while another loop is running' error. If the patch is not applied, the program will run normally.

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

asyncio.set_event_loop_policy(asyncio_gevent.EventLoopPolicy())

def _start_event_loop(loop) -> None:
asyncio.set_event_loop(loop)
loop.run_forever()
loop.close()

def _get_threaded_loop():
new_loop = asyncio.new_event_loop()
thread_loop = threading.Thread(target=_start_event_loop, args=(new_loop,), daemon=True)
thread_loop.start()
return new_loop

async def test_async():
while True:
await asyncio.sleep(3)
print("test", time.time())

async def test_async2():
while True:
await asyncio.sleep(3)
print("test2", time.time())

if name == "main":
_loop = _get_threaded_loop()
asyncio.run_coroutine_threadsafe(test_async(), _loop)
asyncio.run(test_async2())

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