简体   繁体   English

带有烧瓶服务器的 Python 脚本需要很长时间才能在 Windows 上退出

[英]Python script with flask server takes too long to exit on Windows

I'm new to Python.我是 Python 的新手。 As a part of a project I'm trying to deploy a Flask server locally, through the Windows command line.作为项目的一部分,我试图通过 Windows 命令行在本地部署 Flask 服务器。

My Python version is 3.6.0.我的 Python 版本是 3.6.0。

The code:编码:

from flask import Flask

app = Flask(__name__)

@app.route('/') def index():
    return '<h1>Hello World!</h1>'

if __name__ == "__main__":
    app.run()

The problem:问题:

It's about killing the script as it runs.这是关于在脚本运行时杀死脚本。 Launching this script with python deploy.py and hitting CTRL + C shuts it off.使用python deploy.py启动此脚本python deploy.py CTRL + C将其关闭。

BUT - if I hit access that '/' route via the browser once or more, and a moment later try to kill the script in the same manner, then it would take about 10 seconds of nothing until it responds and is finally killed.但是 - 如果我通过浏览器一次或多次访问该'/'路由,然后稍后尝试以相同的方式终止脚本,那么它将需要大约 10 秒的时间,直到它响应并最终被终止。

Why is this happening?为什么会这样? How can I shut the server off immediately each time for continuous and quick development?如何每次都立即关闭服务器以进行持续快速的开发?

Thanks!!谢谢!!

Well if your goal is continuous and quick development, then you can change flask's configuration.好吧,如果您的目标是持续快速开发,那么您可以更改flask 的配置。

Best solution for your problem would be setting the DEBUG setting to True .您问题的最佳解决方案是将DEBUG设置设置为True If DEBUG is set to True, then flask will automatically reload the server on code changes.如果DEBUG设置为 True,flask 将在代码更改时自动重新加载服务器。

There are a few ways to do this but the easiest one(because you said you are a beginner) is to pass the debug argument to app.run()有几种方法可以做到这一点,但最简单的方法(因为你说你是初学者)是将debug参数传递给app.run()

from flask import Flask

app = Flask(__name__)

@app.route('/') 
def index():
    return '<h1>Hello World!</h1>'

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

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

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