'raise RuntimeError('You need to use the eventlet server. '

In my project, I created a app:

the website_chat/views.py code:

async_mode = 'eventlet'

import os

from django.http import HttpResponse
import socketio

basedir = os.path.dirname(os.path.realpath(__file__))
sio = socketio.Server(async_mode=async_mode)
thread = None

the website_chat/management/commands/runserver.py:

from django.core.management.commands.runserver import Command as RunCommand

from xxx/website_chat.views import sio


class Command(RunCommand):
    help = 'Run the Socket.IO server'

    def handle(self, *args, **options):
        if sio.async_mode == 'threading':
            super(Command, self).handle(*args, **options)
        elif sio.async_mode == 'eventlet':
            # deploy with eventlet
            import eventlet
            import eventlet.wsgi
            from Qyun.wsgi import application
            eventlet.wsgi.server(eventlet.listen(('', 8002)), application)
        elif sio.async_mode == 'gevent':
            # deploy with gevent
            from gevent import pywsgi
            from Qyun.wsgi import application
            try:
                from geventwebsocket.handler import WebSocketHandler
                websocket = True
            except ImportError:
                websocket = False
            if websocket:
                pywsgi.WSGIServer(
                    ('', 8000), application,
                    handler_class=WebSocketHandler).serve_forever()
            else:
                pywsgi.WSGIServer(('', 8000), application).serve_forever()
        elif sio.async_mode == 'gevent_uwsgi':
            print('Start the application through the uwsgi server. Example:')
            print('uwsgi --http :5000 --gevent 1000 --http-websockets '
                  '--master --wsgi-file django_example/wsgi.py --callable '
                  'application')
        else:
            print('Unknown async_mode: ' + sio.async_mode)

In my wsgi.py:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Qiyun02.settings")

from socketio import Middleware
from website_chat.views import sio
django_app = get_wsgi_application()
application = Middleware(sio, django_app)

But when I runserver I get the bellow error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/middleware.py", line 47, in __call__
    return self.engineio_app.handle_request(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/socketio/server.py", line 360, in handle_request
    return self.eio.handle_request(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/server.py", line 274, in handle_request
    environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/socket.py", line 91, in handle_get_request
    start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/socket.py", line 133, in _upgrade_websocket
    return ws(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/engineio/async_eventlet.py", line 15, in __call__
    raise RuntimeError('You need to use the eventlet server. '
RuntimeError: You need to use the eventlet server. See the Deployment section of the documentation for more information.

But in the website_chat/views.py, I have configured the:

async_mode = 'eventlet'
sio = socketio.Server(async_mode=async_mode)

why I still get this error?

the document is there:https://github.com/miguelgrinberg/python-socketio/blob/master/docs/index.rst#eventlet



Solution 1:[1]

In the end, I do not follow the example of python-socketio, I configure the wsgi.py like bellow:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Qyun.settings")

from socketio import WSGIApp
from website_chat.views import sio
django_app = get_wsgi_application()
application = WSGIApp(sio, django_app)

#
import eventlet
import eventlet.wsgi
eventlet.wsgi.server(eventlet.listen(('', 8000)), application)

It works for this issue now. the port still is 8000, and the management/commands/runserver.py also can be delete now.

Solution 2:[2]

If this error happens when you try to run your app into an uwsgi environment such us:

uwsgi -w myfile:app\
  --http11-socket 127.0.0.1:3333\
  --enable-threads\
  --threads 4\
  --logformat '%(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) "%(referer)" "%(uagent)"'

where myfile.py contains the application:

import socketio
import eventlet
from engineio.async_drivers import gevent
from flask import Flask

sio = socketio.Server(async_mode="gevent_uwsgi", cors_allowed_origins='*', engineio_logger=True)
app = Flask(__name__)

# this generates the uwsgi-runnable application

my_wsgi = socketio.WSGIApp(sio)
app = socketio.Middleware(sio, my_wsgi)

If you have errors installing gevent by

pip3 install gevent

probably pip will try to install some of the last gevent versions:

21.1.0, 21.1.1, 21.1.2

This may lead to installing errors depending the operative system you use (some issues with darwin).
To overcome these errors, it also works using simpler, older versions of gevent:

pip3 install gevent==1.5.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 XPLOT1ON
Solution 2