Heroku Flask Tutorial Procfile含义

Mic*_*l K 15 python heroku flask

在heroku 教程中,有一段代码

hello.py

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'
Run Code Online (Sandbox Code Playgroud)

和一个Procfile:

web: gunicorn hello:app --log-file=-

真正令人困惑的hello:app部分是部分; 确实hello引用了hello()函数或hello.py脚本?根据其含义,整个Procfile语句的含义是什么?

fre*_*zed 8

提到的Heroku教程不再可用,但Gunicorn的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])
Run Code Online (Sandbox Code Playgroud)

您现在可以使用以下命令运行应用程序:

$ gunicorn --workers=2 test:app


我们试试看,我的测试目录如下所示:

(.venv) 14:41 ~/testgunicorn % tree
.
??? requirements.txt
??? testpkg
    ??? __init__.py
    ??? testfile.py
Run Code Online (Sandbox Code Playgroud)

__init__.py :

from flask import Flask
from .testfile import app
Run Code Online (Sandbox Code Playgroud)

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])
Run Code Online (Sandbox Code Playgroud)

错误的电话:

(.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    
Run Code Online (Sandbox Code Playgroud)

好的召唤:

(.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
(…)
Run Code Online (Sandbox Code Playgroud)

然后为此Procfile:

web: gunicorn hello:app --log-file=-

hello是指hello()函数还是hello.py脚本?为了hello.py脚本

根据其含义,整个Procfile语句的含义是什么?

Heroku的Procfile格式文档说:

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
Run Code Online (Sandbox Code Playgroud)

根据这个答案,它是登录Heroku的标准输出的一个选项.


Rob*_*kal 6

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

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

您的烧瓶应用程序对象是WSGI应用程序,可以使用任何WSGI服务器运行.Gunicorn只是heroku的选择之一.