tl;dr:hello 指的是hello.py,app 指的是app = Flask(__name__)
上面提到的 Heroku 教程不再可用,但是 Gunicorn's doc 给出了一个很好的最小示例:
测试应用示例:
def app(environ, start_response):
"""Simplest possible application object"""
data = b'Hello, World!\n'
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
您现在可以使用以下命令运行应用程序:
$ gunicorn --workers=2 test:app
让我们试试,我的 test-directory 是这样的:
(.venv) 14:41 ~/testgunicorn % tree
.
├── requirements.txt
└── testpkg
├── __init__.py
└── testfile.py
__init__.py:
from flask import Flask
from .testfile import app
testfile.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def app(environ, start_response):
"""Simplest possible application object"""
data = b'Hello, World!\n'
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
打错电话:
(.venv) 14:41 ~/testgunicorn % gunicorn testfile:app
[2018-08-24 14:41:44 +0200] [27248] [INFO] Starting gunicorn 19.9.0
[2018-08-24 14:41:44 +0200] [27248] [INFO] Listening at: http://127.0.0.1:8000 (27248)
[2018-08-24 14:41:44 +0200] [27248] [INFO] Using worker: sync
[2018-08-24 14:41:44 +0200] [27251] [INFO] Booting worker with pid: 27251
[2018-08-24 14:41:44 +0200] [27251] [ERROR] Exception in worker process
Traceback (most recent call last):
File "~/testgunicorn/.venv/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "~/testgunicorn/.venv/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
self.load_wsgi()
File "~/testgunicorn/.venv/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
self.wsgi = self.app.wsgi()
File "~/testgunicorn/.venv/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "~/testgunicorn/.venv/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
return self.load_wsgiapp()
File "~/testgunicorn/.venv/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
return util.import_app(self.app_uri)
File "~/testgunicorn/.venv/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
__import__(module)
ModuleNotFoundError: No module named 'testfile'
[2018-08-24 14:41:44 +0200] [27251] [INFO] Worker exiting (pid: 27251)
[2018-08-24 14:41:44 +0200] [27248] [INFO] Shutting down: Master
[2018-08-24 14:41:44 +0200] [27248] [INFO] Reason: Worker failed to boot.
zsh: exit 3 gunicorn testfile:app
好召唤:
(.venv) 14:43 ~/testgunicorn % gunicorn testpkg:app
[2018-08-24 14:43:56 +0200] [27302] [INFO] Starting gunicorn 19.9.0
[2018-08-24 14:43:56 +0200] [27302] [INFO] Listening at: http://127.0.0.1:8000 (27302)
[2018-08-24 14:43:56 +0200] [27302] [INFO] Using worker: sync
[2018-08-24 14:43:56 +0200] [27305] [INFO] Booting worker with pid: 27305
^C
(…)
(.venv) 15:03 ~/testgunicorn % cd testpkg
(.venv) 15:03 fred@susa ~/git/ocp7/testpkg % gunicorn testfile:app
[2018-08-24 15:03:22 +0200] [27494] [INFO] Starting gunicorn 19.9.0
[2018-08-24 15:03:22 +0200] [27494] [INFO] Listening at: http://127.0.0.1:8000 (27494)
[2018-08-24 15:03:22 +0200] [27494] [INFO] Using worker: sync
[2018-08-24 15:03:22 +0200] [27497] [INFO] Booting worker with pid: 27497
^C
(…)
那么对于这个Procfile:
web: gunicorn hello:app --log-file=-
hello 是指 hello() 函数还是 hello.py 脚本?
到hello.py 脚本
根据其含义,整个 Procfile 语句是什么意思?
Heroku 的 Procfile format documentation 说:
Procfile 在单独的行上声明它的进程类型,每行都有
以下格式:
<process type>: <command>
-
<process type> 是您的命令的字母数字名称,例如 web、worker、urgentworker、clock 等。
-
<command> 表示每个进程类型的dyno在启动时应该执行的命令,例如rake jobs:work。
--logfile=- 选项似乎已被弃用,我在文档中没有找到任何关于它的信息,如果我使用它,我会收到此错误:
(.venv) 15:34 ~/testgunicorn % heroku local web
[WARN] No ENV file found
15:34:30 web.1 | usage: gunicorn [OPTIONS] [APP_MODULE]
15:34:30 web.1 | gunicorn: error: unrecognized arguments: --logfile=-
15:34:30 web.1 Exited with exit code 2
根据this answer,这是一个登录 Heroku 标准输出的选项。