greenlet.error: cannot switch to a different thread

See original GitHub issue

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]$

Issue Analytics

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

github_iconTop GitHub Comments

73reactions
advance512commented, Apr 16, 2017

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!

41reactions
JanBenescommented, Oct 28, 2014

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

greenlet.error: cannot switch to a different thread
Something similar happened to me when I tried to debug a flask application using Pycharm. In vscode the same can be done by...
Read more >
Greenlets and Python Threads - Read the Docs
It is not possible to mix or switch between greenlets belonging to different threads.
Read more >
"greenlet.error: cannot switch to a different thread" in Django ...
I have a django 2 project with gevent monkeypatch in manage.py and wsgi.py (before any other django code): from gevent import monkey monkey.patch_all()....
Read more >
greenlet.error not raised switching between threads for main ...
Consider this code snippet. It starts a background thread and exposes an attribute that allows switching to the main greenlet of the background ......
Read more >
error: cannot switch to a different thread - Google Groups
Deployed web2py to production lately, keep getting an intermittent error "error: cannot switch to a different thread". Please help.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found