Heroku

Heroku 最近加入 support for a process model 它允许部署 Pyramid 应用程序。

这个方法假设在一个名为 myapp . 这种结构可在 pyramid_starter 脚手架和其他粘贴式脚手架(以前称为项目模板)。它可以很容易地修改为与其他PythonWeb应用程序一起工作,也可以通过更改命令来适当地运行应用程序。

第0步:安装Heroku

安装Heroku宝石 per their instructions .

步骤1:添加Heroku所需的文件

您将需要添加以下文件,其中的内容将显示在项目目录的根目录(包含 setup.py

requirements.txt

您可以使用以下命令自动生成此文件。

$ pip freeze > requirements.txt

在你 requirements.txt file, you will probably have a line with your project's name in it. 它可能看起来像下面两行中的任何一行,这取决于您如何设置项目。如果这些行中有任何一行存在,请删除它们。

1project-name=0.0
2
3# or
4
5-e git+git@xxxx:<git username>/xxxxx.git....#egg=project-name

备注

您只能使用可以与PIP一起安装的软件包(例如,pypi上的软件包、git repo中的软件包、使用git+git://url等)。如果您有任何需要以某种特殊方式安装的设备,则必须在 run 文件(见下文)。还要注意,这将在每个实例启动时完成,因此需要快速完成,以避免被Heroku杀死(有60秒的实例启动超时)。部署到Heroku时,不要包含可编辑的引用。

Procfile

生成 Procfile with the following command.

$ echo "web: ./run" > Procfile

run

创造 run with the following command.

#!/bin/bash
set -e
python setup.py develop
python runapp.py

备注

确保 chmod +x run 在继续之前。这个 develop step is necessary because the current package must be installed before Paste can load it from the INI file.

runapp.py

如果使用的版本大于或等于1.3(例如:>=1.3),请使用以下内容 runapp.py .

 1import os
 2
 3from paste.deploy import loadapp
 4from waitress import serve
 5
 6if __name__ == "__main__":
 7    port = int(os.environ.get("PORT", 5000))
 8    app = loadapp('config:production.ini', relative_to='.')
 9
10    serve(app, host='0.0.0.0', port=port)

For versions of Pyramid prior to 1.3 (e.g. < 1.3), use the following for runapp.py .

 1import os
 2
 3from paste.deploy import loadapp
 4from paste import httpserver
 5
 6if __name__ == "__main__":
 7    port = int(os.environ.get("PORT", 5000))
 8    app = loadapp('config:production.ini', relative_to='.')
 9
10    httpserver.serve(app, host='0.0.0.0', port=port)

备注

We assume the INI file to use is named production.ini ,因此更改 runapp.py 必要时。将忽略ini的server部分,因为服务器需要侦听OS环境中提供的端口。

Step 2: Setup git repo and Heroku app

导航到项目目录(目录 setup.py ) if not already there. 如果您的项目已经受Git版本控制,请跳到“初始化Heroku堆栈”部分。

在项目的目录中,如果在Git下不跟踪此项目,建议您选择创建一个好的 .gitignore 文件。您可以通过运行以下命令获得推荐的python命令。

$ wget -O .gitignore https://raw.github.com/github/gitignore/master/Python.gitignore

Once that is done, run the following command.

$ git init
$ git add .
$ git commit -m "initial commit"

步骤3:初始化heroku堆栈

$ heroku create --stack cedar

步骤4:部署

要部署新版本,请将其推送到Heroku。

$ git push heroku master

确保启动一名工人。

$ heroku scale web=1

查看应用程序是否正在运行。

$ heroku ps

如果需要,请查看日志以调试任何错误。

$ heroku logs -t

提示和技巧

CherryPyWSGi服务器快速、高效、多线程,可以轻松地同时处理许多请求。如果你想使用它,你可以添加 cherrpypastescript 对你 setup.py:requires 部分(确保重新运行 pip freeze 如上文所述更新requirements.txt文件)并设置 runapp.py 如下所示。

 1import os
 2
 3from paste.deploy import loadapp
 4from paste.script.cherrypy_server import cpwsgi_server
 5
 6if __name__ == "__main__":
 7    port = int(os.environ.get("PORT", 5000))
 8    wsgi_app = loadapp('config:production.ini', relative_to='.')
 9    cpwsgi_server(wsgi_app, host='0.0.0.0', port=port,
10                  numthreads=10, request_queue_size=200)

Heroku插件通常通过OS环境变量来通信其设置。如下面的示例所示,可以很容易地将这些设置合并到应用程序设置中。

 1# In your pyramid apps main init
 2import os
 3
 4from pyramid.config import Configurator
 5from myproject.resources import Root
 6
 7def main(global_config, **settings):
 8    """ This function returns a Pyramid WSGI application.
 9    """
10
11    # Look at the environment to get the memcache server settings
12    memcache_server = os.environ.get('MEMCACHE_SERVERS')
13
14    settings['beaker.cache.url'] = memcache_server
15    config = Configurator(root_factory=Root, settings=settings)
16    config.add_view('myproject.views.my_view',
17                    context='myproject.resources.Root',
18                    renderer='myproject:templates/mytemplate.pt')
19    config.add_static_view('static', 'myproject:static')
20    return config.make_wsgi_app()