Python 如何在后台运行 Flask Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36465899/
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
How to run Flask Server in the background
提问by Gary
I have set up Flask on my Rapsberry Pi and I am using it for the sole purpose of acting as a server for an xml file which I created with a Python script to pass data to an iPad app (iRule).
我已经在我的 Rapsberry Pi 上设置了 Flask,我使用它的唯一目的是充当我使用 Python 脚本创建的 xml 文件的服务器,以将数据传递给 iPad 应用程序 (iRule)。
My RPI is set up as headless and my access is with Windows 10 using PuTTY, WinSCP and TightVNC Viewer.
我的 RPI 设置为无头,我的访问是使用 PuTTY、WinSCP 和 TightVNC 查看器通过 Windows 10 进行的。
I run the server by opening a terminal window and the following command:
我通过打开终端窗口和以下命令来运行服务器:
sudo python app1c.py
This sets up the server and I can access my xml file quite well. However, when I turn off the Windows machine and the PuTTY session, the Flask server shuts down!
这设置了服务器,我可以很好地访问我的 xml 文件。但是,当我关闭 Windows 机器和 PuTTY 会话时,Flask 服务器关闭了!
How can I set it up so that the Flask server continues even when the Windows machine is turned off?
如何设置它以便 Flask 服务器即使在 Windows 机器关闭时也能继续运行?
I read in the Flask documentation:
我在 Flask 文档中读到:
While lightweight and easy to use, Flask's built-in server is not suitable for production as it doesn't scale well and by default serves only one request at a time.
虽然轻量级且易于使用,但 Flask 的内置服务器不适合生产,因为它不能很好地扩展并且默认一次只处理一个请求。
Then they go on to give examples of how to deploy your Flask application to a WSGI server! Is this necessary given the simple application I am dealing with?
然后他们继续举例说明如何将 Flask 应用程序部署到 WSGI 服务器!鉴于我正在处理的简单应用程序,这是否有必要?
回答by Oz123
You have multiple options:
您有多种选择:
- Easy: deattach the process with
&
, for example:
- 简单:使用 来分离进程
&
,例如:
$ sudo python app1c.py &
$ sudo python app1c.py &
Medium: install tmux with
apt-get install tmux
launch tmux and start your app as before and detach with CTRL+B.Complexer: Read run your flask script with a wsgi server - uwsgi, gunicorn, nginx.
中:安装 tmux 并
apt-get install tmux
启动 tmux 并像以前一样启动您的应用程序,然后使用 CTRL+B 分离。Complexer:阅读使用 wsgi 服务器运行您的烧瓶脚本 - uwsgi、gunicorn、nginx。
回答by Hemanth GS
Use:
用:
$sudo python app1c.py >> log.txt 2>&1 &
$sudo python app1c.py >> log.txt 2>&1 &
">> log.txt" pushes all your stdout inside the log.txt file (You may check the application logs in it)
"2>&1" pushes all the stderr inside the log.txt file (This would push all the error logs inside log.txt)
"&" at the end makes it run in the background.
“>> log.txt”将所有标准输出推送到 log.txt 文件中(您可以检查其中的应用程序日志)
"2>&1" 将所有 stderr 推送到 log.txt 文件中(这会将所有错误日志推送到 log.txt 中)
末尾的“&”使它在后台运行。
You would get the process id immediately after executing this command with which you can monitor or verify it.
执行此命令后,您将立即获得进程 ID,您可以使用它来监视或验证它。
$sudo ps -ef | grep <process-id>
$sudo ps -ef | grep <process-id>
Hope it helps..!!
希望能帮助到你..!!
回答by Peixiang Zhong
Install Node package foreverat here https://www.npmjs.com/package/forever
Then use
在这里永远安装 Node 包https://www.npmjs.com/package/forever
然后使用
forever start -c python your_script.py
to start your script in the background. Later you can use
在后台启动您的脚本。稍后您可以使用
forever stop your_script.py
to stop the script
停止脚本
回答by Gal Mal
Use:
用:
$ sudo nohup python app1c.py > log.txt 2>&1 &
nohup
allows to run command/process or shell script that can continue running in the background after you log out from a shell.
nohup
允许运行命令/进程或 shell 脚本,这些脚本可以在您从 shell 注销后继续在后台运行。
> log.txt
: it forword the output to this file.
> log.txt
:它将输出转发到此文件。
2>&1
: move all the stderr to stdout.
2>&1
: 将所有 stderr 移动到 stdout。
The final &
allows you to run a command/process in background on the current shell.
final&
允许您在当前 shell 的后台运行命令/进程。
回答by Michael Mezher
I've always found a detached screen process to be best for use cases such as these.
Run:
screen -m -d sudo python app1c.py
我一直发现分离屏幕进程最适合此类用例。跑:
screen -m -d sudo python app1c.py
回答by Dhrumil Panchal
You can always use nohup to run any scripts as background process.
您始终可以使用 nohup 将任何脚本作为后台进程运行。
nohup python script.py
This will run your script in background and also have its logs appended in nohup.out file which will be located in the directory script.py is store.
这将在后台运行您的脚本,并将其日志附加在 nohup.out 文件中,该文件将位于目录 script.py is store 中。
Make sure, you close the terminal and not press Ctrl + C. This will allow it to run in background even when you log out.
确保关闭终端而不是按 Ctrl + C。这将允许它在您退出时在后台运行。
To stop it from running , ssh in to the pi again and run ps -ef |grep nohup
and kill -9 XXXXX
where XXXX is the pid you will get ps command.
要阻止它运行,请再次通过 ssh 连接到 pi 并运行 ps -ef |grep nohup
,kill -9 XXXXX
其中 XXXX 是 pid,您将获得 ps 命令。