简体   繁体   English

将 Multiprocessing 与 Python Flask 2.7 一起使用会挂起主应用程序,直到进程完成

[英]Using Multiprocessing with Python Flask 2.7 hangs the main app until process completes

I have set up a Python (2.7) Flask application which uses Flask-SocketIO.我已经建立了一个使用 Flask-SocketIO 的 Python (2.7) Flask 应用程序。 I'm running the app somewhat like this我正在运行这个应用程序有点像这样


eventlet.monkey_patch(socket=True, select=True, subprocess=True)
socketio = SocketIO(app, cors_allowed_origins='*',
                    async_mode='eventlet')
socketio.run(app, host='0.0.0.0', debug=False,
                 port=5100)


Now, the app runs perfectly fine.现在,该应用程序运行良好。 I however for 1 API call, need to leverage Python Microprocessing module.然而,对于 1 个 API 调用,我需要利用 Python 微处理模块。

Here is that API method这是API方法



        jobs = []
        queue = Queue()

        for idx, val in enumerate(myArray):

            process = pool(target=self.getEachNode,
                              args=(val['ip'], self.subscriberId, queue))
            jobs.append(process)


        try:
            print('waiting for someone to inqueue')
            result = queue.get(timeout=120)
            print('finally someone inqueued..................')
            print(result)
        except Exception as ex:
            print('Exception occurred while waiting for queue')

        # terminate all jobs
        for j in jobs:
            j.terminate()

        for j in jobs:
            j.join()

myArray will have like 10 items. myArray 将有 10 个项目。 So this request will run 10 times.所以这个请求会运行 10 次。

Whenever that happens, The python console tells me that a new app has been started (as the following code is run again)每当发生这种情况时,python 控制台都会告诉我一个新应用程序已启动(因为再次运行以下代码)


if __name__ == "__main__":

    app.wsgi_app = DispatcherMiddleware(
        app.wsgi_app, {getenv("API_BASEURL", "/api"): app})
    socketio.run(app, host='0.0.0.0', debug=False,
                 port=5100, use_reloader=False)

print('python server running on port 5100')

Now what actually happens is, when these processes are running, the main app hangs.现在实际发生的是,当这些进程运行时,主应用程序挂起。 I cannot access any other API method.我无法访问任何其他 API 方法。 It basically blocks.它基本上是阻塞的。 This also results in socket disconnection because server gets blocked from my understanding.这也会导致套接字断开连接,因为服务器被我的理解阻止了。

All I want is for those 10 tasks to execute asynchronously.我想要的只是让这 10 个任务异步执行。 Should I use Pool for this?我应该为此使用Pool吗?

Pool has a method of apply_async , will that help? Pool 有一个apply_async方法,有帮助吗?

Also, I just want 1 response, I dont need all of them to complete (no need to Promise.all).另外,我只想要 1 个响应,我不需要全部完成(不需要 Promise.all)。

I cannot use any of Python 3 feature by the way.顺便说一句,我不能使用任何 Python 3 功能。

I ended up using Celery / Message Queue which allows us to manage async tasks in Flask easily.我最终使用了 Celery / Message Queue,它允许我们轻松管理 Flask 中的异步任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM