为什么 Flask 应用程序会创建两个进程? [英] Why does a Flask app create two process?

查看:23
本文介绍了为什么 Flask 应用程序会创建两个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Flask 应该创建一个线程和第二个线程来运行它,但我看到的是总是有两个进程在运行,而不是线程.即使是最简单的应用.

As far as I understood Flask should create a thread and a second thread to run on it, but what I see is there are always two processes, not threads, running. Even for the simplest app.

from flask import Flask
from flask import render_template, request, flash, session, redirect

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

app.run(host="192.168.21.73", port=5000, debug=True)

可以看到有两个进程在运行:

You can see two process running:

ps -x
5026 ttyO0    S+     0:01 /usr/bin/python ./test_flask.py
5031 ttyO0    Sl+    0:45 /usr/bin/python ./test_flask.py

这里发生了什么?

推荐答案

这是因为您正在使用重新加载器运行开发服务器.重新加载器监控文件系统的变化,并在不同的进程中启动真正的应用程序,所以总共有两个进程.

It's because you're running the dev server with the reloader. The reloader monitors the filesystem for changes and starts the real app in a different process, so there are two total processes.

您可以通过在调用 run 时设置 debug=Falseuse_reloader=False 来禁用重载器.

You can disable the reloader by settting debug=False or use_reloader=False when calling run.

这篇关于为什么 Flask 应用程序会创建两个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆