1

I am trying to handle multiple concurrent requests using flask_socketio and eventlet. However, it does not work as expected: When function test1() is running, it blocks the execution of function test2() as seen in the output log.

How can I achieve that the server handles both requests simultaneously?

Server (Python):

import eventlet
eventlet.monkey_patch()

from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit

app = Flask(__name__)
socketio = SocketIO(app, async_mode='eventlet')

@socketio.on('test1')
def test1():
    print('test1 started')
    do_complicated_calculation() # takes some time
    print('test1 done')

@socketio.on('test2')
def test2():
    print('test2')

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

Client (JavaScript):

import io from 'socket.io-client';

socket = io('http://localhost:5000');
socket.emit('test1');
socket.emit('test2');

Expected Output:

test1 started
test2
test1 done

Actual Output:

test1 started
test1 done
test2

1 Answer 1

3

As discussed on GitHub, you need to insert socketio.sleep(0) calls as often as you can inside your long computation, ideally inside a loop so that it happens at regular intervals. That will allow the eventlet scheduler to give the CPU to your second task while the first task is running.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.