简体   繁体   English

使用flask + uwsgi + nginx将flask部署到服务器时,多进程无法在flask中工作

[英]Multiprocess can not work in flask when I deploy flask to server by using flask + uwsgi + nginx

I'm using flask + uwsgi + nginx to deploy website on server.我正在使用 Flask + uwsgi + nginx 在服务器上部署网站。

In the flask, my code is below, here is what I want to design: every time when I click run model, it would run a model in another process, but the interface would link to the waiting interface immediately.在flask中,我的代码在下面,这里是我想要设计的:每次点击run model时,它会在另一个进程中运行一个模型,但界面会立即链接到等待界面。

train_dataset.status = status
db.session.commit()
text = 'Start training your models, please wait for a while or check results several hours later'

# would run a model in another process
task = Process(target=start_train, args=(app.config['UPLOAD_FOLDER'], current_user.id, p.id, p.task), name="training.exe")
task.start()
print(task.pid, task.name)
session[f"{project_name}_train"] = task.pid

# in the meanwhile, link to waiting interface
return render_template('p_computeview.html', project_name=project_name, text=text,
                               status=status, p=p, email=email, not_identify=not_identify)

And when I test in local development environment当我在本地开发环境中测试时

app.run()

it's ok, when I click run model, the interface just link to wait interface and I can see the model running logs.没关系,当我点击运行模型时,界面只是链接到等待界面,我可以看到模型运行日志。 But when I deploy to server, I chose uwsgi + nginx + flask.但是当我部署到服务器时,我选择了uwsgi + nginx + flask。

In uwsgi.ini, I already specify the processes在uwsgi.ini中,我已经指定了进程

processes=2
threads=5

But when I click run model, the interface was still, didn't link to waiting interface, however I can see the model running logs, and when finished modeling, the interface would link to waiting interface (which prove that the Process function was not working ??)但是当我点击运行模型时,界面还是,没有链接到等待界面,但是可以看到模型运行日志,建模完成后,界面会链接到等待界面(证明Process功能没有在职的 ??)

my server have 2 cpus, so I think it can support multi process我的服务器有 2 个 CPU,所以我认为它可以支持多进程

Can someone help me ?有人能帮我吗 ? I guess there are some problems in uwsgi or nginx ?我猜 uwsgi 或 nginx 有问题?

The desire to run separate threads or processes within a request context is a common one.在请求上下文中运行单独的线程或进程是一种常见的愿望。 For various reasons, except in very narrow circumstances, it is a desire that leads to frustration.由于各种原因,除了在非常狭隘的情况下,这是一种导致挫折的愿望。 In this case, as soon as task goes out of scope, the Process machinery gets torn down.在这种情况下,一旦task超出范围, Process机制就会被拆除。

If you want to start a long-running task from a request handler, use a framework like celery or Rq , which arrange to run jobs entirely out of process from the http server.如果您想从请求处理程序启动一个长时间运行的任务,请使用像celeryRq这样的框架,它们可以安排在 http 服务器的进程外运行作业。

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

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