2

I'm trying to run a Flask application with SocketIO using uWSGI and gevent.

uwsgi --gevent 10 --socket :5000 --module run

However, I get the following error:

invalid request block size: 21573 (max 4096)...skip

This is my code:

from gevent import monkey
monkey.patch_all()

from flask import Flask, render_template, session, request
from flask.ext.socketio import SocketIO, emit, disconnect

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

socketio = SocketIO(app)

@app.route('/')
def index():
    session['user'] = '1'
    return render_template('index.html', name='simon')

@socketio.on('my event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']})

@socketio.on('connect', namespace='/test')
def test_connect():
    emit('my response', {'data': 'Connected %s' % session['user']})

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')

if __name__ == '__main__':
    app.debug = True
    socketio.run(app)
nathancahill
  • 10,452
  • 9
  • 51
  • 91

2 Answers2

3

The problem is that you are using binary uwsgi protocol but accessing your server via http protocol. Try replacing --socket with --http-socket. See also https://stackoverflow.com/a/32894820/179581

Community
  • 1
  • 1
Andrei
  • 10,918
  • 12
  • 76
  • 110
1

My understanding is that the gevent support in uWSGI does not allow a custom gevent server class to be used, uWSGI provides its own. Unfortunately gevent-socketio needs its own server, which is subclassed from gevent's, so I think it is currently not possible to use uWSGI with Flask-SocketIO or gevent-socketio.

See the Flask-SocketIO documentation for alternatives.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thank you for your clear explanation. So what about use only gevent module instead of Flask-SocketIO in my Flask application and use proxy nginx? – Šimon Kostolný Feb 23 '15 at 18:53
  • That could possibly work, but you will need to handle the socket communication yourself, sounds like a lot of work. Much easier to use gunicorn+nginx. – Miguel Grinberg Feb 23 '15 at 19:43