0

My code (Python 3.5 on Raspbian 9 - Stretch) is divided up into a number of separate processes, which are run from main.py. A simplified example of my code is below, which I believe is plain vanilla use of Flask, socketIO, eventlet with multiprocessing.Process. The problem is it hangs when I try to access the pipe that connects the different processes.

My understanding (which wouldn’t surprise me if I was completely wrong) is that this is a long standing issue related to eventlet and multiprocessing.Process and as of January 2018 has not been resolved. How to combine multiprocessing and eventlet https://github.com/eventlet/eventlet/issues/147

My question is this. This seems like a common use case, but doesn’t work. So, what work around or different approach would you recommend?

--- in webprocess.py ---

#!/usr/bin/python3
def WebFunc(outfrompipe, intopipe):
    global thread

    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'secret!'
    socketio = SocketIO(app, async_mode=”eventlet”)
    thread = None
    thread_lock = Lock()

    @app.route('/')
    def index():
        return render_template('index.html', async_mode=socketio.async_mode)

    @socketio.on('my_event', namespace='/test')
    def test_msg(msg):
        # Receive a message from a web app
        print(“Received message”, msg)
        # Send this message to another process
        # THIS IS WHERE IT HANGS!!
        intopipe.send(msg)

    socketio.run(app, debug=False, host='0.0.0.0')

--- in main.py ---

#!/usr/bin/python3
import webprocess as webproc
import multiprocessing
import time

if __name__ == '__main__':

    multiprocessing.set_start_method('spawn')
    outfrompipe, intopipe = multiprocessing.Pipe()

    wf = multiprocessing.Process(name=”WebProc”, target=webproc.WebFunc,
        args=(outfrompipe, intopipe))
    wf.start()

    while True:
        message = outfrompipe.recv()
        print(message)
        time.sleep(1)

    wf.join()
FarNorth
  • 289
  • 1
  • 7
  • 16

0 Answers0