Abhay Patil
Abhay Patil

Reputation: 399

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

When I am using multiprocessing module to establish a socket connection between a flask server and a client, the flask server fails to load the pages (routes).

Here is a minimal reproducible code:

Client.py

from multiprocessing.connection import Listener

port_number = 9000

listener = Listener(('localhost', port_number), authkey=b'secret password') ------------------
conn = listener.accept() ------------------
print("connection accepted form flask server") ------------------

while True:
    msg = conn.recv()
    print(msg)

Server.py

from multiprocessing.connection import Client
from flask import Flask

port_number = 9000

conn = Client(('localhost', port_number), authkey=b'secret password')
print("connection established with client")
conn.send("first message")

app = Flask(__name__)


@app.route("/")
def welcome():
    return "hello world"


if __name__ == '__main__':
    app.run(debug=True)

In the above codes, if client.py is run and then server.py is run, then the flask app's home page does not return "hello world".

But when the 3 marked lines are commented in the client.py file, i.e., when the sockets are not used for connection, then the above-mentioned issue does not occur.

In the above-mentioned codes, if the socket connection from the server.py is made with any other files, then also the flask app does not work.

What could be the reason for this?? I am unable to find some obvious logical reason for this.

Upvotes: 3

Views: 1167

Answers (1)

Artiom  Kozyrev
Artiom Kozyrev

Reputation: 3826

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 Client
from flask import Flask
from threading import Thread
import time

FLASK_PORT = 9000
PR_PORT = 8000


def 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 Listener

PORT = 8000

if __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)

Upvotes: 2

Related Questions