烧瓶中的Python多处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35616639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:41:11  来源:igfitidea点击:

Python multiprocessing in flask

pythonflaskparallel-processing

提问by Corbbin Goldsmith

This question has probably been asked, and more than likely answered, but I don't know where to find it here.

这个问题可能已经被问过,而且很有可能得到了回答,但我不知道在哪里可以找到它。

Problem: I have a router for python's flask, that takes a while to process data for each call. I need to make each of the calls to the routes be a thread in itself so it doesn't have to wait for the requests to be loaded.

问题:我有一个用于 python 烧瓶的路由器,每次调用都需要一段时间来处理数据。我需要使对路由的每个调用本身都是一个线程,因此它不必等待加载请求。

采纳答案by Brendan Abel

Flaskcomes with a built-in development web server, but you shouldn't be using it in production.

Flask带有内置的开发 Web 服务器,但您不应该在生产中使用它

To get cool features like separate processes for each request and static file serving, you need to run an actual web service and a WSGI service in front of your Flaskapplication.

要获得每个请求的单独进程和静态文件服务等很酷的功能,您需要在Flask应用程序前面运行一个实际的 Web 服务和一个 WSGI 服务。

The Flaskdocs provide several examples on how to set that up. Popular Web Server/WSGI combinations are Apache/mod_wsgi and Nginx/Gunicorn, but there are many other options.

Flask文档提供了关于如何设置了几个例子。流行的 Web 服务器/WSGI 组合是 Apache/mod_wsgi 和 Nginx/Gunicorn,但还有许多其他选项。

回答by Neil P

A really good way of setting this up would be to use "uwsgi" as your application server (and protocol) and Nginx as your front-end proxy. These are super quick, scalable, handle threading, and it is one of the Flask-recommended methods. Though the flask documentation provides the basic config, this guide is one I've used and it gives a much-more in-depth walkthrough for installation. They are using Ubuntu, but with minor changes (to the install commands) it will work on most Linux flavors.

一个非常好的设置方法是使用“uwsgi”作为您的应用程序服务器(和协议)和 Nginx 作为您的前端代理。这些是超级快速、可扩展、处理线程的方法,它是 Flask 推荐的方法之一。尽管flask 文档提供了基本配置,但本指南是我使用过的指南,它提供了更深入的安装演练。他们使用的是 Ubuntu,但稍作改动(安装命令),它将适用于大多数 Linux 版本。

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04

回答by DankMasterDan

As others have mentioned, there are specialized tools to do this (celeryseems to be the best), but if someone just wants to quickly get something set up and working, here is my approach which only uses Python's multiprocessingmodule:

正如其他人所提到的,有专门的工具可以做到这一点(芹菜似乎是最好的),但如果有人只想快速设置和工作,这是我的方法,它只使用 Python 的multiprocessing模块:

from flask import Flask
from multiprocessing import Process
import time

app = Flask(__name__)

def detachedProcessFunction(wait_time):
    i=0
    while i<wait_time:
        i = i+1
        print "loop running %d" % i
        time.sleep(1)

@app.route('/start')
def start():
    global p
    p = Process(target=detachedProcessFunction, args=(15))
    p.start()
    return render_template('layout.html')

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

NOTE: This method won't work for running object functions (e.g. obj.objFunc()). You will get an EOFError: Ran out of input inside a class. In this case, you should create the object inside a non-object/standalone function and pass the arguments you need to create the object. For more info see here

注意:此方法不适用于运行对象函数(例如obj.objFunc())。你会得到一个EOFError: Ran out of input inside a class. 在这种情况下,您应该在非对象/独立函数中创建对象并传递创建对象所需的参数。有关更多信息,请参见此处