Construct new event loop on gevent workers

53 views Asked by At

I'm trying to run flask server by gunicorn with gevent workers. But my some logics use asyncio's event loop. I created a new thread and ran a newly created asyncio event loop on that thread. But I've encountered an error saying the event loop is already running. The belows are what I met.

$ gunicorn -b 0.0.0.0:9090 -e FLASK_ENV=dev main:app --workers 4 -k gevent
class Foo:
    def __init__(self): 
        self._loop = asyncio.new_event_loop()
        self._thread = threading.Thread(target=self._loop.run_forever, daemon=True)
        self._thread.start()
Exception in thread Thread-3:
Traceback (most recent call last):
  File "/Users/user/.pyenv/versions/3.8.17/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/user/.pyenv/versions/3.8.17/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/user/.pyenv/versions/3.8.17/lib/python3.8/asyncio/base_events.py", line 560, in run_forever
    self._check_running()
  File "/Users/user/.pyenv/versions/3.8.17/lib/python3.8/asyncio/base_events.py", line 554, in _check_running
    raise RuntimeError(
RuntimeError: Cannot run the event loop while another loop is running

Question

Q. It based on gevent worker, but why does asyncio's event loop say it's already running?

hypothesis

  1. Gevent is based on asyncio. So there are some conflicts.
  2. Gevent monkey-patch some logics of asyncio at runtime.
  3. gunicorn do not run normaly.
0

There are 0 answers