简体   繁体   English

Python / Flask 后台进程死机

[英]Python / Flask background process dies

NB.注意。 This is a repost of the question.这是问题的转贴。 My original post was closed because someone thought I asked about how I could configure a Flask app for production or to run in background.我的原始帖子已关闭,因为有人认为我问我如何配置 Flask 应用程序以用于生产或在后台运行。 This is not the question.这不是问题。 The question is why the python background process suddenly dies.问题是为什么 python 后台进程突然死掉了。

I am running a Flask application on Ubuntu (using development server) with the following command line:我正在使用以下命令行在 Ubuntu(使用开发服务器)上运行 Flask 应用程序:

python myflaskapp.py > log.txt 2>&1 & python myflaskapp.py > log.txt 2>&1 &

After I log off the ssh session - it runs fine for a while but then suddenly the process dies.在我注销 ssh session 之后 - 它运行了一段时间但突然进程停止了。 There are no requests to it that causes it either.也没有导致它的请求。 It just dies.它只是死了。 The application is really simple.该应用程序非常简单。 There are no DB connections or anything.没有数据库连接或任何东西。 It just receives some file upload and uses XLRD to convert it to csv.它只是接收一些文件上传并使用 XLRD 将其转换为 csv。 But it dies after a while even if no requests are sent to it.但是即使没有请求发送给它,它也会在一段时间后死亡。

Any ideas what causes this?任何想法是什么原因造成的? Any way of logging it?有什么办法记录吗? There are no log entries in log.txt that shows whats going on either. log.txt 中也没有显示正在发生的事情的日志条目。

I know that I am not supposed to run development server for production, and I am running it also in gunicorn now - but I would really like to know why the Python process dies.我知道我不应该为生产运行开发服务器,而且我现在也在 gunicorn 中运行它 - 但我真的很想知道为什么 Python 进程会死掉。

I have another Flask application and that one "lives forever".我有另一个 Flask 应用程序,那个应用程序“永远存在”。 It is built in the same way - but that process will live until the computer boots or some unhandled exception occurs.它以相同的方式构建 - 但该过程将一直存在,直到计算机启动或发生一些未处理的异常。

I think I found the issue here.我想我在这里找到了问题。 It seems that systemd is killing the session after some time - even if running the process in background.一段时间后,systemd 似乎正在杀死 session - 即使在后台运行该进程。

There are (at least) two solutions:有(至少)两种解决方案:

  • Set KillUserProcesses=No in logind.conf在 logind.conf 中设置 KillUserProcesses=No
  • Run as systemd service instead改为作为 systemd 服务运行

https://fedoraproject.org/wiki/Changes/KillUserProcesses_by_default https://fedoraproject.org/wiki/Changes/KillUserProcesses_by_default

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

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