Flask服务器运行得很好,但是与gunicorn一起运行时,显示以下错误:
[2020-07-08 14:58:33 +0000] [27561] [INFO] Starting gunicorn 20.0.4
[2020-07-08 14:58:33 +0000] [27561] [INFO] Listening at: http://172.20.31.202:5000 (27561)
[2020-07-08 14:58:33 +0000] [27561] [INFO] Using worker: sync
[2020-07-08 14:58:33 +0000] [27564] [INFO] Booting worker with pid: 27564
[2020-07-08 14:58:34 +0000] [27564] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
...
...
File "/root/customer-account-automation/wsgi.py", line 7, in <module>
from app import app as application
File "/root/customer-account-automation/app.py", line 6, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
[2020-07-08 14:58:34 +0000] [27564] [INFO] Worker exiting (pid: 27564)
[2020-07-08 14:58:34 +0000] [27561] [INFO] Shutting down: Master
[2020-07-08 14:58:34 +0000] [27561] [INFO] Reason: Worker failed to boot.
我并不怀疑软件包的安装,因为它已经可以与flask run
一起使用。
这是wsgi.py
代码
#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
from app import app as application
if __name == "__main__":
application.run()
,pipenv site-packages目录同时包含requests
和gunicorn
,如下所示:
(customer-account-automation) root@jsd-user-management:~/customer-account-automation# ll /root/.local/share/virtualenvs/customer-account-automation-gLS21FFx/lib/python3.7/site-packages/
...
drwxr-xr-x 7 root root 4096 Jul 8 15:14 gunicorn/
drwxr-xr-x 2 root root 4096 Jul 8 15:14 gunicorn-20.0.4.dist-info/
drwxr-xr-x 3 root root 4096 Jul 8 14:39 requests/
drwxr-xr-x 2 root root 4096 Jul 8 14:39 requests-2.24.0.dist-info/
drwxr-xr-x 4 root root 4096 Jul 8 14:39 requests_oauthlib/
drwxr-xr-x 2 root root 4096 Jul 8 14:39 requests_oauthlib-1.3.0.dist-info/
drwxr-xr-x 10 root root 4096 Jul 8 14:39 requests_toolbelt/
drwxr-xr-x 2 root root 4096 Jul 8 14:39 requests_toolbelt-0.9.1.dist-info/
...
如果gunicorn无法导入请求但flask run
成功了,那会是什么?
可能与pipenv有关吗?
答案 0 :(得分:1)
错误似乎来自工作进程。这些可能会在python中打开子进程,并且可能会在您的系统上调用python的错误版本-该版本未安装请求。
gunicorn的config允许您在运行时指定python路径的附加内容。
class PythonPath(Setting):
name = "pythonpath"
section = "Server Mechanics"
cli = ["--pythonpath"]
meta = "STRING"
validator = validate_string
default = None
desc = """\
A comma-separated list of directories to add to the Python path.
e.g.
``'/home/djangoprojects/myproject,/home/python/mylibrary'``.
"""
您可以在运行gunicorn时尝试添加请求模块的位置。您可以通过运行pip show requests
来查找请求的位置。
> pip show requests
Name: requests
Version: 2.23.0
...
Location: /root/.local/share/virtualenvs/customer-account-automation-gLS21FFx/lib/python3.7/site-packages/
然后将该位置附加到您用于gunicorn的任何命令上:
gunicorn --pythonpath /root/.local/share/virtualenvs/customer-account-automation-gLS21FFx/lib/python3.7/site-packages/