0

I'm trying to use flask-socketio to handle the communications between a server and a client. The server code can be boiled down to this:

from multiprocessing import Process, freeze_support
import eventlet
eventlet.monkey_patch()
freeze_support()

from flask_socketio import SocketIO
from flask import Flask, request, session
from time import time

app = Flask(__name__)

sio = SocketIO(app, logger=True, engineio_logger=True, async_mode='eventlet')

@sio.on('create_process')
def create_process():
    test_process = Process(target=process_stuff)
    test_process.start()

def process_stuff():
    print("Process Initialized")
    start_time = time()
    while time() - start_time < 60:
        # process stuff
        continue
    print("Process Terminated")

sio.run(app, "0.0.0.0", 8000)

It's all working perfectly at the beginning but, after some time, multiprocessing stops creating new processes. The server, however, continues to receive events. I have also tried using gevent async_mode but this didn't fix the problem.

My actual script never sends or handles events directly in the processes, it's completely separate. Is this still some sort of limitation with using multiprocessing and flask-socketio?

  • Flask-SocketIO does not really care about multiprocessing. Your problem is likely an incompatibility between eventlet/gevent and multiprocessing. If you are going to use any form of concurrency outside of eventlet's own, problems are likely to occur. I really care about multiprocessing, then I suggest you stop using eventlet and/or gevent, which use their own style of concurrency based on greenlets. – Miguel Grinberg Jul 09 '23 at 08:13
  • I mean to say, "If you really care about multiprocessing, then drop eventlet/gevent, as that will eliminate these incompatibilities". Sorry for the typo above. – Miguel Grinberg Jul 09 '23 at 12:11
  • Thank you for your input, I would really like to take advantage of flask socketio’s handling of multiple clients and asynchronous events, for that it seems to require either eventlet or gevent as a backend… – Tiago Martinez Jul 09 '23 at 15:41
  • You can use threads for concurrency as well without gevent or eventlet. – Miguel Grinberg Jul 09 '23 at 18:32

0 Answers0