Heroku Flask 教程 Procfile 含义

Heroku Flask Tutorial Procfile Meaning

herokututorial里面有一段代码

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 语句的含义是什么?

ProcFile 包含用于在 heroku 上启动应用程序的命令行。完整的文档可以在这里找到:https://devcenter.heroku.com/articles/procfile

在这种情况下,它告诉 heroku 使用 hello 模块中的 app 变量(你构建的 flask 应用程序)和 gunicorn 并启动一个 web 进程(一个可以处理 http 请求的进程)。您还可以指定其他进程类型,例如后台工作人员。

您的 flask 应用程序对象是一个 WSGI 应用程序,并且可以运行 使用任何 WSGI 服务器。 Gunicorn 只是 heroku 上的选择之一。

tl;dr:hello 指的是 hello.pyapp 指的是 app = Flask(__name__)


提到的 Heroku 教程不再可用,但是 Gunicorn's doc 给出了一个很好的最小示例:

Example with the test app:

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])

You can now run the app with the following command:

$ 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=-

Does hello refer to the hello() function or the hello.py script?

hello.py 脚本

Depending on the meaning of that, what does the whole Procfile statement mean?

Heroku 的 Procfile format documentation 说:

A Procfile declares its process types on individual lines, each with the following format:

<process type>: <command>

  • <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=- 选项似乎已被弃用,我在文档中没有找到任何关于它的信息,如果我使用它,我会收到此错误:

(.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 标准输出的选项。

1- 根据 Heroku doc 部分 Procfile 格式

web: gunicorn hello:app  

是<进程类型>:<命令>模式

2- web 因此是 。根据相同 documentation:“Heroku 应用程序的 Web 进程类型是唯一可以从 Heroku 的路由器接收外部 HTTP 流量的进程类型。如果您的应用程序包含 Web 服务器,则应将其声明为应用程序的 Web 进程。 “

3- 现在是 部分:

gunicorn hello:app  

如果您查看 gunicorn doc 部分 基本用法 ,您将看到 gunicorn 的典型命令是

$ gunicorn [OPTIONS] APP_MODULE

其中 APP_MODULE 的格式为 $(MODULE_NAME):$(VARIABLE_NAME).

因此在您的示例中 hello 指的是 $(MODULE_NAME) 即 hello.py。请注意,如有必要,它可以是完整的虚线路径。以同样的方式 app 引用一个 WSGI 可调用 $(VARIABLE_NAME) 应该在指定的 hello 模块中找到并且在您实例化 Flask class 时实际定义: app = Flask(__name__).