Flask-SocketIO gets stuck as soon as the program is run

Created on 12 May 2017  路  4Comments  路  Source: miguelgrinberg/Flask-SocketIO

Sorry if this is a rookie mistake or if the title is too ambiguous, I couldn't think of any other way to ask this question!

I've been working on a Flask project using flask-socketio for a while, and had the server running properly on my Macbook, but upon trying to run the code on either a Windows PC or a Raspberry Pi running Raspbian, the program seems to get stuck immediately. I type python app.py into the terminal (which worked on Mac) and it advances one line, which stays blank, and gets stuck until I ctrl-C. I get the same issue when running as FLASK_APP=app.py flask run.

I've found I get the same issue just by running the example code on the flask-socketio page:

from flask import Flask, render_template
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

if __name__ == '__main__':
    socketio.run(app)

But somehow the program runs fine when the final line is replaced with app.run(). I should mention that the program runs fine without either eventlet or gevent installed, so having them installed seems to have something do with the error.

If there is any advice about how to solve this issue I would be very grateful! Thank you! (Oh, and sorry if this is the wrong forum to post this in!)

question

Most helpful comment

Just a sanity check - are you sure that your server isn't running? If you have neither gevent not eventlet installed, Flask will run with the development server, which displays several lines of output at startup.

When I try with eventlet, there is no output except a single blank line. However, the server does seem to be running correctly. Can you try and talk to your app and see if the server responds?

To check this with the Flask-SocketIO sample app, I went to http://127.0.0.1:5000 in a browser and got a 'Not Found' error message, which is correct. I also went to http://127.0.0.1:5000/socket.io/ and got a message that I could download a binary file.

All 4 comments

can you post the output of pip freeze, in your setup where you have eventlet and gevent installed and it doesn't work?

Just a sanity check - are you sure that your server isn't running? If you have neither gevent not eventlet installed, Flask will run with the development server, which displays several lines of output at startup.

When I try with eventlet, there is no output except a single blank line. However, the server does seem to be running correctly. Can you try and talk to your app and see if the server responds?

To check this with the Flask-SocketIO sample app, I went to http://127.0.0.1:5000 in a browser and got a 'Not Found' error message, which is correct. I also went to http://127.0.0.1:5000/socket.io/ and got a message that I could download a binary file.

But somehow the program runs fine when the final line is replaced with app.run()

Have you tried connecting to the server? I'm pretty sure that is not going to work well if you start the server with app.run().

I should mention that the program runs fine without either eventlet or gevent installed, so having them installed seems to have something do with the error.

When you install eventlet or gevent, the web server that is used comes from the package you installed and not from Flask/Werkzeug. The output is going to be different, and you may even get no output, depending on how logging is configured.

As @jwg4 said, regardless of not getting the output you expect, connect to the server to decide if it is running or not, looking at the output is not a good measure of success.

Thank you so much for the detailed responses! It looks like the server was working (enough) all along, and I had just assumed it was frozen because it wasn't outputting anything on the system I had moved to.

(Just for clarity, when I said it ran "fine" without eventlet/gevent installed, I meant that the command line would happily say that the server started and what address to connect to, and then give a server error page when I connected)

Was this page helpful?
0 / 5 - 0 ratings