Flask server not working when there is an active socket connection using multiprocessing module Flask server not working when there is an active socket connection using multiprocessing module flask flask

Flask server not working when there is an active socket connection using multiprocessing module


Just move conn.send("first message") to another thread and change port for Flask app, your code above blocks Flask server from running.

your server:

from multiprocessing.connection import Clientfrom flask import Flaskfrom threading import Threadimport timeFLASK_PORT = 9000PR_PORT = 8000def thread_target_func():    """send all this actions to another thread"""    conn = Client(('localhost', PR_PORT), authkey=b'secret password')    print("connection established with client")    try:        # let's add while loop to male client to be busy with smth        # otherwise you'll get error in client soon - EOF        while True:            conn.send("first message")            time.sleep(5)    except Exception as ex:        print(ex)app = Flask(__name__)@app.route("/")def welcome():    return "hello world"if __name__ == '__main__':    # start you connection in another Thread    th = Thread(target=thread_target_func)    th.start()    # run Flask app in main thread    app.run(host="0.0.0.0", port=FLASK_PORT)    th.join()

your client:

from multiprocessing.connection import ListenerPORT = 8000if __name__ == '__main__':    listener = Listener(('localhost', PORT), authkey=b'secret password')    conn = listener.accept()    print("connection accepted form flask server")    while True:        msg = conn.recv()        print(msg)