21

I've been running a dockerized flask application that uses Celery to run tasks. To run the app I'm using gunicorn with eventlet and It's been working fine using alpine linux distribution.

However I had to move to ubuntu due to some issues with sklearn and other libraries, and now I'm having problems to run my app.

First of all I'm getting this error:

Error: class uri 'eventlet' invalid or not found:

[Traceback (most recent call last):
  File "/myapp/env/lib/python3.6/site-packages/gunicorn/workers/geventlet.py", line 11, in <module>
    import eventlet
ModuleNotFoundError: No module named 'eventlet'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/myapp/env/lib/python3.6/site-packages/gunicorn/util.py", line 135, in load_class
    mod = import_module('.'.join(components))
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/myapp/env/lib/python3.6/site-packages/gunicorn/workers/geventlet.py", line 13, in <module>
    raise RuntimeError("You need eventlet installed to use this worker.")
RuntimeError: You need eventlet installed to use this worker.

Then I tried by adding pip install eventlet to my app's Dockerfile, and now I have this another error:

Error: class uri 'eventlet' invalid or not found:

[Traceback (most recent call last):
  File "/myapp/env/lib/python3.6/site-packages/gunicorn/util.py", line 135, in load_class
    mod = import_module('.'.join(components))
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/myapp/env/lib/python3.6/site-packages/gunicorn/workers/geventlet.py", line 11, in <module>
    import eventlet
  File "/myapp/env/lib/python3.6/site-packages/eventlet/__init__.py", line 10, in <module>
    from eventlet import convenience
  File "/myapp/env/lib/python3.6/site-packages/eventlet/convenience.py", line 7, in <module>
    from eventlet.green import socket
  File "/myapp/env/lib/python3.6/site-packages/eventlet/green/socket.py", line 21, in <module>
    from eventlet.support import greendns
  File "/myapp/env/lib/python3.6/site-packages/eventlet/support/greendns.py", line 69, in <module>
    setattr(dns.rdtypes.IN, pkg, import_patched('dns.rdtypes.IN.' + pkg))
  File "/myapp/env/lib/python3.6/site-packages/eventlet/support/greendns.py", line 59, in import_patched
    return patcher.import_patched(module_name, **modules)
  File "/myapp/env/lib/python3.6/site-packages/eventlet/patcher.py", line 126, in import_patched
    *additional_modules + tuple(kw_additional_modules.items()))
  File "/myapp/env/lib/python3.6/site-packages/eventlet/patcher.py", line 100, in inject
    module = __import__(module_name, {}, {}, module_name.split('.')[:-1])
  File "/myapp/env/lib/python3.6/site-packages/dns/rdtypes/IN/WKS.py", line 25, in <module>
    _proto_tcp = socket.getprotobyname('tcp')
OSError: protocol not found

This is how I start my app (start.sh):

gunicorn -w 1 --worker-class eventlet --certfile=myapp/myapp.crt --keyfile=myapp/myapp.key --bind 0.0.0.0:5000 --log-config myapp/gunicorn.conf myapp.run:app

It's been working fine until I moved to an ubuntu based container.

What am I missing here?

I appreciate any help.

Thanks!

magnoz
  • 1,939
  • 5
  • 22
  • 42

3 Answers3

28

I had the same issue, and I found that it is caused by some updates made by eventlet they removed eventlet.wsgi.ALREADY_HANDLED but gunicorn is still using it. So , you better downgrade the eventlet version.

pip install gunicorn==20.1.0 eventlet==0.30.2

Here is a reference https://github.com/eventlet/eventlet/issues/702

Mwibutsa Floribert
  • 1,824
  • 14
  • 21
  • 1
    Beware, that this will not work with Python 3.10. Make sure to specify python version by adding a file called runtime.txt with the text "python-3.9.13". That worked for me. – Blue Print Jun 17 '22 at 15:43
  • I use poetry. pyproject.toml: `python = "3.8.1"` `gunicorn = {extras = ["eventlet==0.30.2"], version = "^20.1.0"}` but error: `ImportError: cannot import name 'ALREADY_HANDLED' from 'eventlet.wsgi'` – Kuznetsov-M Jul 18 '22 at 14:22
  • 2
    @BluePrint Why won't it work in 3.10? – Fabien Snauwaert Nov 24 '22 at 14:13
2

I tried for a lot and finally working, please check these:

If u are using heroku, edit the requeriments.txt adding gunicorn and eventlet version and also edit runtime.txt with "python-3.9.13" (In case you are using heroku stack 22). If you are running another heroku stack, please check it's python compatible versions. (source = https://devcenter.heroku.com/articles/python-support#supported-runtimes)

1

I'll post the solution I've found in case it's useful to anyone.

Just I was missing the /etc/protocols, and this is how I've fixed it:

I had to add this step in my Dockerfile:

RUN apt-get -o Dpkg::Options::='--force-confmiss' install --reinstall -y netbase

magnoz
  • 1,939
  • 5
  • 22
  • 42
  • 1
    I am facing the same problem, but this does not resolves my issue. I am running this command. `gunicorn app.instance:app --worker-class eventlet -w 1 --bind 0.0.0.0:5000 --reload`. Any help would be appreciated. – Gurkiran Singh Mar 19 '21 at 15:34