问题描述
在 heroku 教程中,有一段代码
In the heroku tutorial, there is a piece of code
hello.py
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World!'
和Procfile:
web: gunicorn hello:app --log-file=-
真正令人困惑的部分是 hello:app
部分;hello
是指 hello()
函数还是 hello.py 脚本?根据其含义,整个 Procfile 语句是什么意思?
The part that's really confusing is the hello:app
part; does hello
refer to the hello()
function or the hello.py script? Depending on the meaning of that, what does the whole Procfile statement mean?
推荐答案
tl;dr: hello
指的是 hello.py
和 app
指的是 app = Flask(__name__)
tl;dr: hello
refers to hello.py
and app
refers to app = Flask(__name__)
提到的 Heroku 教程不再可用,但是 Gunicorn 的文档给出了一个很好的最小例子:
The mentioned Heroku tutorial is no more available, however Gunicorn's doc gives a good minimal example :
测试应用示例:
def app(environ, start_response):
"""Simplest possible application object"""
data = b'Hello, World!
'
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
您现在可以使用以下命令运行应用程序:
You can now run the app with the following command:
$ gunicorn --workers=2 test:app
让我们试试,我的测试目录看起来像这样:
(.venv) 14:41 ~/testgunicorn % tree
.
├── requirements.txt
└── testpkg
├── __init__.py
└── testfile.py
__init__.py
:
__init__.py
:
from flask import Flask
from .testfile import app
testfile.py
:
testfile.py
:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def app(environ, start_response):
"""Simplest possible application object"""
data = b'Hello, World!
'
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 格式文档 说:
Heroku's Procfile format documentation says :
Procfile 在单独的行上声明它的进程类型,每一行都有以下格式:
:
是您的命令的字母数字名称,例如 web、worker、emergencyworker、clock 等.
表示进程类型的每个 dyno 在启动时应该执行的命令,例如 rake jobs:work.
<process type>
is an alphanumeric name for your command, such as web, worker, urgentworker, clock, and so on.<command>
indicates the command that every dyno of the process type should execute on startup, such as rake jobs:work.
--logfile=-
选项似乎已被弃用,我在文档中没有找到有关它的任何内容,如果我使用它,我会收到此错误:
The --logfile=-
option seems to be deprecated, I did not find anything about it in documentation and if I use it I get this error :
(.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
根据这个答案,这是登录 Heroku 标准输出的一个选项.
According to this answer it was an option for logging in Heroku's stdout.
这篇关于Heroku Flask 教程 Procfile 含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!