Nick
Nick

Reputation: 406

Passing socket as an argument in Flask-SocketIO multiprocessing

I am trying to get a Flask-SocketIO server working with multiprocessing. The server works fine without multiprocessing. I need to emit SocketIO messages inside a function invoked by accessing a route. Previously, I did this by passing socketio as an argument to the function, and then using socketio.emit(), which worked fine. Unfortunately, when I add the function to the pool with pool.apply(), I get TypeError: cannot serialize 'greenlet.greenlet' object. If I don't pass the socket, it works but obviously I don't get the socket functionality that I need. What is causing this error and how do I correct it? I'm not using gunicorn or eventlet or gevent or anything else.

My simplified code:

def requester(user, socket):

    socket.emit('hello')
    # do some stuff with requests and databases
    socket.emit('goodbye')

def main():

    app = Flask(__name__,
                static_url_path='',
                static_folder='dist',
                template_folder='dist')

    socketio = SocketIO(app)

    pool = multiprocessing.Pool(16)

    @app.route('/addname', methods=['POST'])
    def add_name:
        pool.apply(requester, args=(request.json['user'], socketio))
        return request.json['user'], 201

    socketio.run(app, host='0.0.0.0', port=8000)

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 1553

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67517

I'm not using gunicorn or eventlet or gevent or anything else.

You clearly are using a greenlet based framework. The selection of async framework is automatic, so by having eventlet or gevent installed on your virtualenv greenlets are being used. If you don't want to use these, make sure neither eventlet or gevent are installed.

I am trying to get a Flask-SocketIO server working with multiprocessing.

This isn't going to work because Socket.IO is a stateful server, you can't scale up by just running multiple worker processes.

See the documentation on emitting from an external process for the recommended method to emit to clients from auxiliary processes.

Upvotes: 2

Related Questions