如何在本地使用 Python 应用程序运行/测试 Gunicorn?

问题描述 投票:0回答:2

我继承了一个 Python 项目,由于

Worker terminated due to signal 9
,它在我们的管道中失败了。

我认为它与 Gunicorn 有关,但我不确定如何在本地复制它。我尝试阅读 Gunicorn 文档,但没有意义。

Gunicorn 位于

requirements.txt
文件中。我在项目中没有看到引用/导入 Gunicorn 的任何地方。最接近的文件是
config.py

它说安装了 Gunicorn,我应该能够使用

gunicorn
命令,但这对我不起作用。它说该命令不存在或类似的内容。

python gunicorn
2个回答
0
投票

这可行,但我仍然需要弄清楚如何关闭它......你可以发送信号,但我更喜欢更好的东西。

pip install gunicorn

然后在py文件中:

from _pytest.monkeypatch import MonkeyPatch
from unittest import mock

import threading
import gunicorn.app.wsgiapp

def gunicornapp():
    args = ['gunicorn', 'main:app']
    MonkeyPatch().setenv('FLASK_SECRET_KEY', 'hello')
    MonkeyPatch().setenv('FLASK_ENV', 'development')
    with mock.patch.object(sys, 'argv', args):
        return gunicorn.app.wsgiapp.WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]")

app = gunicornapp()
thread = threading.Thread(target=app.run())
thread.start()

0
投票

首先确保您的项目目录中有一个

wsgi.py
文件。

其次确保你安装了gunicorn:

gunicorn --version

如果没有:

pip install gunicorn

然后你可以运行:

gunicorn your_project_name.wsgi:application

例如我的项目名为 django_project,所以我运行:

gunicorn django_project.wsgi:application
© www.soinside.com 2019 - 2024. All rights reserved.