Flask-SocketIO: greenlet.error: cannot switch to a different thread

Hi!

I installed Flask-SocketIO on my Fedora and the following error displays :

[vincent@localhost controller]$ python server.py 
 * Running on http://0.0.0.0:5000/
 * Restarting with reloader
Unhandled exception in thread started by <function run_server at 0x8ee456c>
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask_socketio/__init__.py", line 242, in run_server
    self.server.serve_forever()
  File "/usr/lib/python2.7/site-packages/gevent/baseserver.py", line 284, in serve_forever
    self._stop_event.wait()
  File "/usr/lib/python2.7/site-packages/gevent/event.py", line 77, in wait
    result = self.hub.switch()
  File "/usr/lib/python2.7/site-packages/gevent/hub.py", line 331, in switch
    return greenlet.switch(self)
greenlet.error: cannot switch to a different thread

*server.py : *

# -*-coding: utf-8 -*-
from flask import Flask
from flask.ext.socketio import SocketIO, emit

from controller import Controller
from map import Map
from doser import Doser
from locator import Locator

app = Flask(__name__)
app.debug = True
socket = SocketIO(app)

@socket.on("load-map")
def load_map(data):
    map_ = Map(data)

    # Notify modules
    if map_.is_valid():
        emit("map", map_.data) # Client
    else:
        emit("map", {}) # Client

@app.route("/")
def home():
    with open("templates/home.html", "r") as f:
        return f.read()

if __name__ == "__main__":
    # app.run(host="0.0.0.0")
    socket.run(app, host="0.0.0.0")

I get the same message on Raspbian.

Is it due to Flask-SocketIO ?

Thanks!

Edit : with debug mode disabled, it seems to work :

[vincent@localhost controller]$ python server.py 
127.0.0.1 - - [2014-10-26 13:05:42] "GET / HTTP/1.1" 200 1142 0.014441
^CKeyboardInterrupt
Traceback (most recent call last):
  File "server.py", line 31, in <module>
    socket.run(app, host="0.0.0.0")
  File "/usr/lib/python2.7/site-packages/flask_socketio/__init__.py", line 247, in run
    self.server.serve_forever()
  File "/usr/lib/python2.7/site-packages/gevent/baseserver.py", line 284, in serve_forever
    self._stop_event.wait()
  File "/usr/lib/python2.7/site-packages/gevent/event.py", line 77, in wait
    result = self.hub.switch()
  File "/usr/lib/python2.7/site-packages/gevent/hub.py", line 331, in switch
    return greenlet.switch(self)
KeyboardInterrupt
[vincent@localhost controller]$

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 26 (9 by maintainers)

Commits related to this issue

Most upvoted comments

If you see this in the context of debugging using PyCharm, try this:

Go to Settings --> Build, Execution, Deployment -> Python Debugger. In that dialog, you’ll see a “Gevent compatible” checkbox. Not sure how it got unticked in the new project.

Tick that option and enjoy!

I would like to add that I monkey patch only in the __main__ guard and that works as well.

if __name__ == '__main__':
    from gevent import monkey
    monkey.patch_all()

For me, this is important so that Celery, which imports some of my scripts, wouldn’t hang, see https://github.com/miguelgrinberg/Flask-SocketIO/issues/61.

Finally, if you’re using PyCharm/PyDev, enabling File->Settings…->Python Debugger->Gevent compatible debugging might be required.

Sadly this appears to be a side effect of the removal of the gevent monkey-patching. It appears that is needed when running the reloader. Add the following two lines at the top of your server.py, above all the imports:

from gevent import monkey
monkey.patch_all()

It is important that these are above the imports, in lines 1 & 2. Let me know if this addresses the problem for you.

There is a gevent configuration option for vscode that needs to be True. See https://code.visualstudio.com/docs/python/debugging#_gevent. In my experience this option also allows eventlet apps to be debugged.

If you see this in the context of debugging using PyCharm, try this:

Go to Settings --> Build, Execution, Deployment -> Python Debugger. In that dialog, you’ll see a “Gevent compatible” checkbox. Not sure how it got unticked in the new project.

Tick that option and enjoy!

This!