【问题标题】:Running subprocess in gunicorn without ssh shell leads to 500 Internal Server Error在没有 ssh shell 的 gunicorn 中运行子进程会导致 500 Internal Server Error
【发布时间】:2016-03-03 09:53:30
【问题描述】:

我在 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 登录,一切都运行良好...而且我还设置了 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 &

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

【问题讨论】:

    标签: python-3.x nginx flask gunicorn


    【解决方案1】:

    使用 ssh 登录后,我运行以下命令:

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

    只要我在我的 ssh 会话中,一切正常。关闭它后,我的网站仍然可以访问,但是...

    你不能那样运行 gunicorn。它是您的 ssh 会话的一部分,当您注销时它会被终止(特别是,它会丢失其控制终端并获得 SIGHUPped)。

    您可以通过使用 --daemon 标志更好地使 gunicorn 背景本身更好地解决此问题,但您应该真正考虑从适当的进程控制系统(例如 systemdsupervisor)运行 gunicorn .

    【讨论】:

    • 谢谢!这就是问题所在。我尝试了 deamon + supervisor,两者都运行得非常好:)
    • @Speedy,对于主管(与大多数过程控制系统一样),最好使用--daemon。守护进程将 gunicorn 与其父进程分离(这就是它在命令行上提供帮助的原因),这意味着主管无法轻松跟踪 gunicorn 是否仍在运行。
    • 是的,这正是我的意思,我做了 2 次尝试:一次使用 gunicorn 和 --deamon,一次使用主管 + gunicorn(没有守护程序)。也许我的第一条评论会导致误解。但两种方式都有效,我现在正在与主管一起运行它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多