(翻译玩)在使用flask-script的应用上使用gunicorn
在使用flask-script的应用上使用gunicorn
两周前,我强烈的想要学习一点新知识,像新的语言,新的框架之类的!好让我的大脑忙碌起来,寻找了一些日子后,我决定学习现在越来越流行的云应用平台(Paas).因为我认为实战比看教程和阅读文档更有效,我决定写一个flask web程序并在Heroku上部署,经过一段时间的学习与编程后,我想要在Gunicorn生产环境中运行它.
那么,怎么让WSGI应用与gunicorn一起运行呢?在一般的情况下是很简单的,只需要运行
gunicorn appmodule:app
这样就ok了,然而我开发时使用了像如下这样的工厂模式:(一个返回Flask程序实例的函数,Flask-Manager就使用了这个模式)
def create_app(environment='development'):
app = Flask(__name__)
...
return app
flask-script提供了一个可以轻松管理你工作环境的方法,例如你可以轻松的通过下面代码运行服务
python manage.py runserver --environment development
你可以通过参数方便的配置程序,就像Heroku通过修改Procfile文件来配置环境一样
web: python manage.py runserver --environment development
这是非常好的解决方案,不幸的是它并没有向flask开发服务的外部提供任何东西,因此我使用gunicorn时不能像下面这样配置代码
python manage.py rungunicorn --environment production --gunicorn-config gunicorn.ini
我唯一能做的是:
gunicorn mymodule:create_app\(environment=\"production\"\)
这太丑了,并切根本没有用到manage.py,完全不能忍,这就是我决定折腾flask-script和gunicorn的原因,在Flask-Action的基础上改写,可以解决我的问题:
class Gunicorn(Command):
description = 'Runs production server with gunicorn'
def __init__(self, host='127.0.0.1', port=5000, **options):
self.port = port
self.host = host
self.server_options = options
def get_options(self):
options = (
Option('--gunicorn-host',
dest='host',
default=self.host),
Option('--gunicorn-port',
dest='port',
type=int,
default=self.port),
Option('--gunicorn-config',
dest='config',
type=str,
required=True))
return options
def handle(self, app, host, port, config, **kwargs):
from ConfigParser import ConfigParser
gc = ConfigParser()
gc.read(config)
section = 'default'
bind = "%s:%s" % (host, str(port))
workers = gc.get(section, 'workers')
pidfile = gc.get(section, 'pidfile')
loglevel = gc.get(section, 'loglevel')
# Suppress argparse warnings caused by running gunicorn's argparser
# "inside" Flask-Script which imports it too...
warnings.filterwarnings("ignore", "^.*argparse.*$")
from gunicorn import version_info
if version_info >= (0, 9, 0):
from gunicorn.app.base import Application
class FlaskApplication(Application):
def init(self, parser, opts, args):
return {
'bind': bind,
'workers': workers,
'pidfile': pidfile,
'loglevel': loglevel
}
def load(self):
return app
# Hacky! Do not pass any cmdline options to gunicorn!
sys.argv = sys.argv[:2]
print "Logging to stderr with loglevel '%s'" % loglevel
print "Starting gunicorn..."
FlaskApplication().run()
else:
raise RuntimeError("Unsupported gunicorn version! Required > 0.9.0")
而gunicorn.ini是这样的:
[default]
workers = 1
pidfile = tmp/districtrank.pid
loglevel = debug
tips
因为gunicornn也使用了argparse,所以如果我移除了Manager的sys.argv参数 gunicorn就会崩溃.也许还有更好的解决方案,但对我的需求来说已经完全足够了.
因为一些关于import的原因,需要清空输出,所以我禁止了argparse的警告,禁止警告虽然不好,但是我认为在这种情况下是可以理解的
manager.add_command("rungunicorn", Gunicorn(host=host,port=port))
现在你可以使用新创建的指令来配置了
这肯定不是一段可以合并到Flask-Script的代码(迎合了我的需求,但是不是太通用,而且使用了丑陋的sys.argv并且禁止了argparse的导入警告),但是它也能正确的运行,所以我在guniorn中使用了
python manage.py rungunicorn --environment production --gunicorn-config gunicorn.ini
Works for me!
(翻译玩)在使用flask-script的应用上使用gunicorn的更多相关文章
- flask+script命令行交互工具
Project name :Flask_Plan templates:templates static:static 首先说,我们flask比django方便的地方是所有的模块都可以自己选,你不喜欢s ...
- Flask script 内的Shell 类 使用
1.集成Python shell 每次自动shell会话都要导入数据库实例和模型,很烦人.为了避免一直重复导入,我们可以做些配置让Flask-Script的Shell命令自动导入特定的对象.若想把对象 ...
- (翻译玩)SQLALchemy backref章节文档
Linking Relationships with Backref 自从在Object Relational Tutorial中第一次提到backref参数后,许多案例中也用到了backref,那么 ...
- 翻译 | 玩转 React 表单 —— 受控组件详解
原文地址:React.js Forms: Controlled Components 原文作者:Loren Stewart 译者:小 B0Y 校对者:珂珂君 本文涵盖以下受控组件: 文本输入框 数字输 ...
- Flask deployment on gunicorn with flask script
https://stackoverflow.com/questions/34265870/flask-deployment-on-gunicorn-with-flask-script 依赖 Flask ...
- dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.
http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术 ...
- Flask下如何处理Requests 上传中文文件名的问题
一.问题的由来 最近有个项目,叫做文档服务资源中心,类似于七牛,为各个业务系统提供统一的文件资源服务,包括文件的存储.操作管理.下载.预览等.在做文件存储的时候,遇到了这个当指定上传的文件名为 ...
- [小北De编程手记] : Lesson 03 玩转 xUnit.Net 之 Fixture(上)
在使用xUnit.Net Framework构建单元测试或自动化测试项目的时候,无论是针对一些比较耗费资源的对象亦或是为了支持Test case预设数据的能力,我们都需要有一些初始化或是清理相关的动作 ...
- flask中下载服务器上特定路径的文件
使用flask下载服务器上某个路径下的文件 path:文件路径以及需要下载的文件,直接写入参数有安全隐患,实际应用中需要判断权限之类的 from flask import send_file, mak ...
随机推荐
- unity 脚本编译顺序
根据官方的解释,它们的编译顺序如下: (1)所有在Standard Assets.Pro Standard Assets或者Plugins文件夹中的脚本会产生一个Assembly-CSharp-fil ...
- 文件下载cordovaFileTransfer:cordova.file.documentsDirectory is null
在Android平台上使用:$cordovaFileTransfer进行文件下载时提示:cordova.file.documentsDirectory is null,查了以下文档参照:http:// ...
- Entity Framework with MySQL 学习笔记一(查询)
参考 : http://msdn.microsoft.com/en-us/data/jj574232.aspx EF 查询基本上有3中 默认是 Lazy Loading 特色是只有在需要数据的时候EF ...
- Largest product in a series
这个我开始理解错了,算错了. 我以为是求连续5个数的最大值,结果,是连接5个数相乘的最大值. 英语不好,容易吃亏啊. Find the greatest product of five consecu ...
- 51单片机C语言学习笔记4:keil C51绝对地址访问
在利用keil进行8051单片机编程的时,常常需要进行绝对地址进行访问.特别是对硬件操作,如DA AD 采样 ,LCD 液晶操作,打印操作.等等.C51提供了三种访问绝对地址的方法: 1. 绝对宏: ...
- 公网IP和私有IP
IP地址是为了区分网络中不同主机所分配的一个地址,通过IP地址可以访问到每一台主机. IP地址分为公有地址和私有地址,公有地址由Internet NIC负责(比如中国互联网信息中心http://ip. ...
- @Valid springMVC bean校验不起作用及如何统一处理校验
SpringMVC 使用JSR-303进行校验 @Valid 使用注解 一.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-vali ...
- 硝烟中的Scrum和XP-我们如何实施Scrum 4 (Part 1/2)
4 制定Sprint计划 计划是Scrum中重要的一环; 是为了让团队获得足够信息, 不受打扰地工作, 增加团队的信心; Planning的成果: 1) Sprint目标 2) 团队成员名单(时间百分 ...
- COJ WZJ的数据结构(负十八)splay_tree的天堂
WZJ的数据结构(负十八) 难度级别:E: 运行时间限制:100000ms: 运行空间限制:700KB: 代码长度限制:2000000B 试题描述 对于前一段样例: 输入 输入文件的第1行包含两个数N ...
- HDU_2057——64位无符号16进制数的运算
Problem Description There must be many A + B problems in our HDOJ , now a new one is coming. Give yo ...