:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; max-width: 100%; height: auto !important; margin: 2px 0; } table { border-collapse: collapse; border: 1px solid #bbbbbb; } td, th { padding: 4px 8px; border-collapse: collapse; border: 1px solid #bbbbbb; } @media only screen and (-webkit-max-device-width: 1024px), only screen and (-o-max-device-width: 1024px), only screen and (max-device-width: 1024px), only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (-o-min-device-pixel-ratio: 3), only screen and (min-device-pixel-ratio: 3) { html, body { font-size: 17px; } body { line-height: 1.7; padding: 0.75rem 0.9375rem; color: #353c47; } h1 { font-size: 2.125rem; } h2 { font-size: 1.875rem; } h3 { font-size: 1.625rem; } h4 { font-size: 1.375rem; } h5 { font-size: 1.125rem; } h6 { color: inherit; } ul, ol { padding-left: 2.5rem; } blockquote { padding: 0 0.9375rem; } }
-->

div{font-size:15px;}.wiz-table-tools .wiz-table-menu-item.active .wiz-table-menu-sub {display: block}.wiz-table-tools .wiz-table-menu-sub:before, .wiz-table-tools .wiz-table-menu-sub:after {position: absolute;content: " ";border-style: solid;border-color: transparent;border-bottom-color: #cccccc;left: 22px;margin-left: -14px;top: -8px;border-width: 0 8px 8px 8px;z-index:10;}.wiz-table-tools .wiz-table-menu-sub:after {border-bottom-color: #ffffff;top: -7px;}.wiz-table-tools .wiz-table-menu-sub-item {padding: 4px 12px;font-size: 14px;}.wiz-table-tools .wiz-table-menu-sub-item.split {border-top: 1px solid #E0E0E0;}.wiz-table-tools .wiz-table-menu-sub-item:hover {background-color: #ececec;}.wiz-table-tools .wiz-table-menu-sub-item.disabled {color: #bbbbbb;cursor: default;}.wiz-table-tools .wiz-table-menu-sub-item.disabled:hover {background-color: transparent;}.wiz-table-tools .wiz-table-menu-item.wiz-table-cell-bg:hover .wiz-table-color-pad {display: block;}.wiz-table-tools .wiz-table-color-pad {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 88px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-color-pad > div{font-size:15px;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item i.pad-demo {position: absolute;top:3px;left:0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item .icon-oblique_line{color: #cc0000;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child {margin-right: 0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item.active i.editor-icon.icon-box {color: #448aff;}.wiz-table-tools .wiz-table-cell-align {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 65px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right:0}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item i.valign{position: absolute;top:3px;left:0;color: #d2d2d2;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.valign {color: #a1c4ff;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.icon-box,.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.align {color: #448aff;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child,.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right: 0;}th.wiz-selected-cell-multi, td.wiz-selected-cell-multi {background: rgba(0,102,255,.05);}th:before,td:before,#wiz-table-col-line:before,#wiz-table-range-border_start_right:before,#wiz-table-range-border_range_right:before{content: " ";position: absolute;top: 0;bottom: 0;right: -5px;width: 9px;cursor: col-resize;background: transparent;z-index:100;}th:after,td:after,#wiz-table-row-line:before,#wiz-table-range-border_start_bottom:before,#wiz-table-range-border_range_bottom:before{content: " ";position: absolute;left: 0;right: 0;bottom: -5px;height: 9px;cursor: row-resize;background: transparent;z-index:100;}.wiz-table-container {}.wiz-table-body {position:relative;padding:0 0 10px;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch;}.wiz-table-body table {margin:0;outline:none;}td,th {height:28px;word-break:break-all;box-sizing:border-box;outline:none;}body pre.prettyprint {padding:0;}body pre.prettyprint code {white-space: pre;}body pre.prettyprint.linenums {box-shadow:none; overflow: auto;-webkit-overflow-scrolling: touch;}body pre.prettyprint.linenums ol.linenums {box-shadow: 40px 0 0 #FBFBFC inset, 41px 0 0 #ECECF0 inset; padding: 10px 10px 10px 40px !important;}
-->

Celery 与 Flask 大型程序结构结合的关键:

使 Celery 与 Flask 结合的关键在于非 Web Runtime 中调取上下文。
因为当 Flask App 在作为 WSGI Application 运行时,会在每个请求进入的时候将请求上下文推入 _request_ctx_stack 中。推入请求上下文的同时,将检查应用上下文堆栈 _app_ctx_stack 是否为空,若空则隐式地推入一个 应用上下文。最终,在请求线程退出前,应用上下文将从其 Flask._app_ctx_stack 的栈中里弹出
Celery 作为任务调度模块,其任务执行单元(worker)运行时,将处于非 Web Runtime 的状态,而此时应用上下文堆栈 Flask._app_ctx_stack 为空,因此调取上下文将引发"RunTimeError"的报错。
Flask 大型程序结构下,采用工厂函数(create_app)创建 Flask App,因此同样采用工厂函数的形式创建 Celery App,其工厂函数如下:
from celery import Celery
                                    
def make_celery(App):
    CeleryInst = Celery(App.import_name, backend = App.config['CELERY_RESULT_BACKEND'], broker = App.config['CELERY_BROKER_URL'])
    CeleryInst.conf.update(App.config)
    TaskBase = CeleryInst.Task
    class ContextTask(TaskBase):
        """Will be execute when create the instance object of ContextTasks."""
        # Will context(Flask's Extends) of app object(Producer Sit)
        # be included in celery object(Consumer Site).
        abstract = True
        def __call__(self, *args, **kwargs):
            with App.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    # Include the app_context into celery.Task.
    # Let other Flask extensions can be normal calls.
    CeleryInst.Task = ContextTask
    return CeleryInst
 
 
注意:此方式创建 Celery 对象时,将会把用于初始化的 Flask App 的上下文信息包含至 Celery 对象中。基于此,Flask 扩展在调用 Celery 任务时,采用最普通的方式,而不需额外考虑上下文问题。
 
Celery 与 Flask 大型程序结构结合的实例:
Flask 大型程序结构如下。
|-flasky
   |-app/
       |-templates/
       |-static/
       |-main/
           |-__init__.py
           |-errors.py
           |-forms.py
           |-views.py
       |-__init__.py
       |-mail/
           |-templates/
           |-__init__.py
           |-mail.py
   |-migrations/
   |-tests/
       |-__init__.py
       |-test*.py
|-venv/
|-requirements.txt
|-config.py
|-manage.py
注意:与原 Flask 大型程序结构作出如下修改以及强调
(1).Flask App 及 Celery App 创建于 /flasky/app/__init__.py 中。
(2).manage.py 中通过 import 方式引入 Flask App,并在该文件中启用 WSGI。
(3).在其他需要调用 Flask App 的地方,采用 current_app._get_current_object() 实现。
 
 
 
 

Celery 与 Flask 大型程序结构的结合的更多相关文章

  1. Flask 学习 六 大型程序结构

    pip freeze >requirement.txt 自动生成版本号 pip install -r requirement.txt 自动下载对应的库 梳理结构 config.py #!/usr ...

  2. [ Python ] Flask 基于 Web开发 大型程序的结构实例解析

    作为一个编程入门新手,Flask是我接触到的第一个Web框架.想要深入学习,就从<FlaskWeb开发:基于Python的Web应用开发实战>这本书入手,本书由于是翻译过来的中文版,理解起 ...

  3. Flask从入门到精通之大型程序的结构一

    尽管在单一脚本中编写小型Web 程序很方便,但这种方法并不能广泛使用.程序变复杂后,使用单个大型源码文件会导致很多问题.不同于大多数其他的Web 框架,Flask 并不强制要求大型项目使用特定的组织方 ...

  4. ASP.NET MVC掉过的坑_MVC初识及MVC应用程序结构

    APS.Net MVC 浅谈[转] 来自MSDN 点击访问 MVC 理论结构 模型-视图-控制器 (MVC) 体系结构模式将应用程序分成三个主要组件:模型.视图和控制器. ASP.NET MVC 框架 ...

  5. 网易云课堂_C语言程序设计进阶_第6周:程序结构

    6.1 全局变量 6.2 编译预处理 6.3 大程序结构 6.1 全局变量 全局变量 定义在函数外面的变量是全局变量 全局变量具有全局的生存期和作用域 它们与任何函数都无关 在任何函数内部都可以使用它 ...

  6. C/C++语言简介之程序结构

    C语言的模块化程序结构用函数来实现,即将复杂的C程序分为若干模块,每个模块都编写成一个C函数,然后通过主函数调用函数及函数调用函数来实现一大型问题的C程序编写,因此常说:C程序=主函数+子函数.因此, ...

  7. [译文]Domain Driven Design Reference(七)—— 大型战略设计结构

    本书是Eric Evans对他自己写的<领域驱动设计-软件核心复杂性应对之道>的一本字典式的参考书,可用于快速查找<领域驱动设计>中的诸多概念及其简明解释. 上周末电脑硬盘文件 ...

  8. [C语言]进阶|程序结构

    ------------------------------------------------------------------------------------ 全局变量: // main.c ...

  9. python 全栈开发,Day142(flask标准目录结构, flask使用SQLAlchemy,flask离线脚本,flask多app应用,flask-script,flask-migrate,pipreqs)

    昨日内容回顾 1. 简述flask上下文管理 - threading.local - 偏函数 - 栈 2. 原生SQL和ORM有什么优缺点? 开发效率: ORM > 原生SQL 执行效率: 原生 ...

随机推荐

  1. Android的方法和属性(1)

    1.Activity常用的方法 View findViewById(int id) //根据组件的ID取得组件对象 setContentView(int layoutResID) //设置布局文件,设 ...

  2. 浅析Java基本数据类型

    作为一种强类型语言,Java针对每一种数据都定义了明确的数据类型.大体来讲可分为:基本数据类型和引用数据类型:在此,主要讨论前者,后者以后再继续探究. Java基本数据类型分为四类八种: 1: 整数 ...

  3. iOS开发之最近开发遇到的问题总结

    1.Cannot create __weak reference in file using manual reference counting 解决办法: 点击工程-------->Build ...

  4. [转]Socket编程中,阻塞与非阻塞的区别

    阻塞:一般的I/O操作可以在新建的流中运用.在服务器回应前它等待客户端发送一个空白的行.当会话结束时,服务器关闭流和客户端socket.如果在队列中没有请示将会出现什么情况呢?那个方法将会等待一个的到 ...

  5. SpringMVC集成rabbitMQ

    Maven引入相关jar <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-c ...

  6. Python 函数 min()

    min() 函数 作用:  min() 方法返回给定参数的最小值,参数可以为序列.x-数值表达式.y-数值表达式.z-数值表达式.返回给定参数的最小值. 语法: min( x, y, z, .... ...

  7. (转)Android 使用com.j256.ormlite

    在web开发中经常采用的hibernate,在android也提供了一个ormlite 导入所需jar包后 摘自:http://blog.csdn.net/cuiran/article/details ...

  8. ubutun

    地址:http://www.cnblogs.com/dutlei/archive/2012/11/20/2778327.html

  9. CIDR地址分类

    CIDR(Classless Inter Domain Routing)改进了传统的IPv4地址分类.传统的IP分类将IP地址直接对应为默认的分类,从而将Internet分割为网络.CIDR在路由表中 ...

  10. mybatis-spring升级导致的异常

    先记录一下,已被后面排查问题 升级前pom.xml依赖信息: <dependency> <groupId>org.mybatis</groupId> <art ...