Flask-SocketIO,事件,多处理进程不兼容

时间:2018-07-22 17:10:43

标签: python python-multiprocessing flask-socketio eventlet

我的代码(Raspbian 9上的Python 3.5-Stretch)分为多个单独的进程,这些进程从main.py运行。下面是我的代码的简化示例,我相信这是Flask,socketIO,带有multiprocessing.Process的eventlet的普通香草用法。问题是当我尝试访问连接不同进程的管道时,它挂起了。

我的理解(如果我完全错了,这也不会让我感到惊讶),这是一个与事件和多处理相关的长期问题。截至2018年1月,Process尚未解决。 How to combine multiprocessing and eventlet https://github.com/eventlet/eventlet/issues/147

我的问题是这个。这似乎是一个普通的用例,但不起作用。那么,您会推荐哪种解决方法或其他方法?

---在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')

---在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()

0 个答案:

没有答案