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

Why does gevent affect the asyncio usage of child thread? #2026

Open
ssppest opened this issue Apr 3, 2024 · 0 comments
Open

Why does gevent affect the asyncio usage of child thread? #2026

ssppest opened this issue Apr 3, 2024 · 0 comments

Comments

@ssppest
Copy link

ssppest commented Apr 3, 2024

  • gevent version: 20.10.2
  • Python version: cPython 3.9
  • Operating System: macOS 14.3.1(M3)

Description:

I use gevent patch for my program, but in the program, I need to execute asyncio related code in sub threads
When multiple sub threads execute the same coroutine, it triggers "RuntimeError: This event loop is already running"
They are different threads, I generated its own event loop for each sub thread,I cannot understand this issue
When I commented out the monkey patch, the program executed as I expected

ERROR:root:This event loop is already running
Traceback (most recent call last):
  File "/Users/computer1/pytest/test.py", line 30, in func1
    loop.run_until_complete(asyncf1())
  File "/opt/homebrew/Cellar/python@3.9/3.9.18_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete
    self._check_running()
  File "/opt/homebrew/Cellar/python@3.9/3.9.18_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 583, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
/Users/computer1/pytest/test.py:32: RuntimeWarning: coroutine 'asyncf1' was never awaited
  logging.error(e, exc_info=True)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
ERROR:root:This event loop is already running
Traceback (most recent call last):
  File "/Users/computer1/pytest/test.py", line 30, in func1
    loop.run_until_complete(asyncf1())
  File "/opt/homebrew/Cellar/python@3.9/3.9.18_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete
    self._check_running()
  File "/opt/homebrew/Cellar/python@3.9/3.9.18_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 583, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

What I've run:

import logging

import gevent.monkey
gevent.monkey.patch_all()
import concurrent.futures

import threading
import time
import asyncio

pool = concurrent.futures.ThreadPoolExecutor()

async def asyncf1():
    print("aa")
def func1():
    # print(f"thread:{threading.get_ident()},gevent:{id(gevent.getcurrent())}")
    try:
        try:
            loop = asyncio.get_event_loop()
        except RuntimeError:
            loop = asyncio.new_event_loop()
            # asyncio.set_event_loop(loop)
        loop.run_until_complete(asyncf1())
    except Exception as e:
        logging.error(e, exc_info=True)
    print(threading.current_thread())
    time.sleep(3)


for i in range(3):
    pool.submit(func1)

time.sleep(10)
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