如何让Intellij-idea正确关闭Flask开发服务器?
我使用 IDEA 10.5 进行 Flask 实验。 Flask 有嵌入式测试服务器(就像 Django 一样)
当我启动测试类时,开发服务器也会在端口 5000 上启动。一切都很好。 * 运行于 http://127.0.0.1:5000/
当我单击“停止进程”按钮时 (红色方块),我收到消息说该过程已完成: 进程已完成,退出代码为 143
但是服务器仍然处于活动状态(响应请求),我可以看到我仍然有一个 python 进程正在运行。
显然这会阻止我立即重新启动测试,我必须首先终止服务器进程。
你如何设法让你的程序和服务器同时结束?
I use IDEA 10.5 for my Flask experimentation. Flask has en embedded test server (like Django does)
When I launch my test class, the dev server launches as well on port 5000. All good.
* Running on http://127.0.0.1:5000/
When I click on the "Stop process" button (red square), I get the message saying the process is finished :
Process finished with exit code 143
However the server is still alive (responds to requests) and I can see I still have a python process running.
Obviously this prevents me from relaunching the test straight away, I have to kill the server process first.
How do you manage to get both your program and the server ending at the same time ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想发生的情况是,您启动了 Flask 应用程序,然后该应用程序将开发服务器分叉为一个新进程。如果您停止应用程序,分叉进程仍在运行。
这看起来是一个无法通过 IDE 轻松解决的问题。在再次启动应用程序之前,您可以在主程序中添加一些内容来终止已经运行的服务器进程,但这看起来很难看。
但为什么不直接使用
app.run(debug=True)
如 Flask 文档中所述?每次您更改应用程序时,服务器都会自动重新加载,因此您无需手动停止并重新启动它。编辑:
我突然想到了一些奇怪的事情:如果您只需要一种舒适的方式从 IDE 中终止服务器,您所要做的就是在重新加载器监视的地方之一引入语法错误,保存文件和服务器会被它噎死:)
I guess what happens is that you start your flask app which then is forking the development server as a new process. If you stop the app the forked process is still running.
This looks like a problem, that cannot easily be solved within the means of your IDE. You could add something to your main to kill the already running server process, before starting the app again, but that seems ugly.
But why don't you just start your app with
app.run(debug=True)
as described in flask doc? The server will reload automatically everytime you changed your app so you don't have to stop and restart it manually.EDIT:
Something a bit quirky just came to my mind: if you just need a comfortable way to kill the server from within the IDE all you have to do is to introduce a syntactical error in one of the places the reloader monitors, save the file and the server will choke on it and die :)
新版本不会再发生这种情况(使用 PyCharm 2.0 测试)
This doesn't happen anymore with newer versions (tested with PyCharm 2.0)