运行 gunicorn 中没有 ssh shell 的子进程导致 500 内部服务器错误

Running subprocess in gunicorn without ssh shell leads to 500 Internal Server Error

我在 python3.4 中尝试了一些烧瓶 当 运行 在 nginx 后面使用 gunicorn 连接我的应用程序并关闭我的 ssh 会话时,我遇到了一些问题 使用 ssh 登录后,我 运行 以下内容:

gunicorn -w 3 -n MultiServer -b 127.0.0.1:8081 app:app &

只要我在 ssh 会话中,一切正常。在我关闭它之后,我的网站仍然可以访问,但是当我点击以下功能时:

def get_audio(self, a_link, a_format="mp3"):
    a_name = str(self._tmpDownloadDir + "/%(title)s.%(ext)s")
    ex = 'youtube-dl -o "'+ a_name +'" --no-playlist --extract-audio --audio-format '+ a_format + ' "' + a_link + '"'
    if subprocess.call(ex, shell=True) == 0:
        a_name = self._moveAudio()
        return (a_link, a_name, a_format)
    else:
        raise UnsuportedFormatException

我遇到内部服务器错误。所有其他网站和功能都正常工作。

完整代码如下:

MultiServer

尤其是: YoutubeService

如前所述,只要我使用 ssh 登录,一切都 运行ning 没问题...而且我还设置了 shell=True。


更新:

好的,我在 app.py:

中尝试了这个
if __name__ == "__main__":
    handler = logging.FileHandler('/tmp/app.log')                             
    handler.setLevel(logging.ERROR) 
    app.logger.addHandler(handler) 
    app.debug = True
    app.run()

但是日志保持为空并且没有调试发生

还修复了 gunicorn 日志:

gunicorn --error-logfile /tmp/app1.log -w 3 -n MultiServer -b 127.0.0.1:8081 app:app &

它也是空的,只是开始了一些关于工人的信息。

After logging in with ssh i run following:

gunicorn -w 3 -n MultiServer -b 127.0.0.1:8081 app:app &

All works fine as long i am in my ssh session. After i close it, my site is still reachable, but ...

你不能像那样 运行 gunicorn。它是您的 ssh 会话的一部分,当您注销时它会被杀死(特别是,它会失去其控制终端并收到 SIGHUPped)。

您可以通过使用 --daemon 标志让 gunicorn 背景本身更好来解决这个问题,但是您真的应该考虑 运行ning gunicorn 来自适当的过程控制系统,例如 systemd主管.