celery 与 flask 实现异步任务调度
Flask 定了2中上下文,来实现机遇线程\协程的,wsgi服务的请求(request、session)和存储(g,current_app )过程,通过栈来完成不同线程和协程的上下文切换,在与celery相结合处理异步任务时,需要保证异步任务在同一个上下文中执行,需要对celery进行重构, 避免出现:
ile "/opt/xAssets/venv/lib/python3.6/site-packages/celery/app/trace.py", line 412, in trace_task
R = retval = fun(*args, **kwargs)
File "/opt/xAssets/venv/lib/python3.6/site-packages/celery_context/model.py", line 42, in __call__
with current_app.app_context():
File "/opt/xAssets/venv/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/opt/xAssets/venv/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/opt/xAssets/venv/lib/python3.6/site-packages/flask/globals.py", line 51, in _find_app
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
celery 的使用
- 应用:
from celery import Celery
celery = Celery('xassets',
broker=settings.CELERY_BROKER_URL,
include="xassets.tasks",
backend=settings.CELERY_RESULT_BACKEND,
)
- celery 以 默认方式(perfork)
# 紧接上文
from celery.signals import worker_process_init
TaskBase = celery.Task class ContextTask(TaskBase):
abstract = True def __call__(self, *args, **kwargs):
with current_app.app_context():
return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask @worker_process_init.connect
def init_celery_flask_app(**kwargs):
"""Create the Flask app after forking a new worker. This is to make sure no resources are shared between processes.
"""
app = create_app()
app.app_context().push()
# 注意: worker_process_init 信号,仅用于perfork方式启动,如果以gevent,greelet 或者thread方式启动,这此处不执行,也就是会缺少缺少app实例化
- 以gevent方式启动(大多数人的选择)
TaskBase = celery.Task app = create_app() class ContextTask(TaskBase):
abstract = True def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask
# 注意:1. 需要提前创建 flask 上下文应用实例
2. 重定义task时,建议使用app.app_context
celery_context 封装一键化应用
from celery_context import Celery
celery = Celery('xassets',
broker=settings.CELERY_BROKER_URL,
include="xassets.tasks",
backend=settings.CELERY_RESULT_BACKEND,
)
app = create_app()
celery.reload_task(app)
# 或者
# celery.init_app(app)
celery 与 flask 实现异步任务调度的更多相关文章
- Celery 与 Flask 大型程序结构的结合
:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...
- flask + celery实现定时任务和异步
参考资料: Celery 官网:http://www.celeryproject.org/ Celery 官方文档英文版:http://docs.celeryproject.org/en/latest ...
- Python Flask后端异步处理(二)
在实际的应用场景中,如用户注册,用户输入了注册信息后,后端保存信息到数据库中,然后跳转至登录界面,这些操作用户需要等待的时间非常短,但是如果是有耗时任务,比如对输入的网址进行漏洞扫描,在后端处理就会花 ...
- celery(芹菜) 异步任务 定时任务 周期任务
什么是celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 celery架构 celery的架构由三部分组成,消息中间件(me ...
- Python Flask后端异步处理(三)
前一篇博文我们已经将基础知识和环境配置进行了介绍:https://www.cnblogs.com/Cl0ud/p/13192925.html,本篇博文在实际应用场景中使用Celery,对Flask后端 ...
- Flask(python)异步(ajax)返回json格式数据
主要讨论两个问题,第一个是关于json.dumps 与jsonify区别,第二个是几种异步的区别(见jQuery中的$.getJSON.$.ajax.$.get.$.post的区别). json.du ...
- Flask实现异步非阻塞请求功能
pip install gevent 关于gevent Gevent 是一个 Python 并发网络库,它使用了基于 libevent 事件循环的 greenlet 来提供一个高级同步 API.下面是 ...
- flask 实现异步非阻塞----gevent
我们都知道,flask不支持异步非阻塞的请求,我们可以创建一个新项目去测试一下,推荐大家使用pycharm去开发我们的flask 使用特别的方便. rom flask import Flask im ...
- Celery(一个懂得 异步任务、定时任务、周期任务 的"芹菜")
一.什么是Celery? Celery 是基于Python实现的模块,用于执行异步.定时.周期任务的,其结构的组成是: - 用户任务 app - 管道 broker 用于存储任务(官方推荐 redis ...
随机推荐
- 9.PowerShell DSC之Pull
前言 一般生产环境都使用Pull模式 配置Pull Server 配置Pull Server需要安装两个WindowsFeture:IIS.windows DSC,这两都可以通过UI界面化引导安装,也 ...
- Linux-压缩/解压缩命令
目录 Linux系统中常见的压缩包格式 gzip 压缩命令 zip 压缩命令 tar 压缩命令(归档) Linux系统中常见的压缩包格式 格式 压缩工具 .zip zip压缩工具 .gz gzip压缩 ...
- C++11特性-右值引用
什么是左值,什么是右值 常见的误区有 = 左边的是左值,右边的是右值. 左值:具有存储性质的对象,即lvalue对象,是指要实际占用内存空间.有内存地址的那些实体对象,例如:变量(variables) ...
- Spring(一)概述
Spring 的前世今生 相信经历过不使用框架开发 Web 项目的 70 后.80 后都会有如此感触,如今的程序员开发项目太轻松 了,基本只需要关心业务如何实现,通用技术问题只需要集成框架便可.早在 ...
- Docker项目demo
Docker数据持久化 4.1 Volume (1)创建mysql数据库的container (2)查看volume (3)具体查看该volume docker volume inspect 485 ...
- IIS6.0(CVE-2017-7269) 缓冲器溢出
漏洞描述开启WebDAV服务对IIS 6.0存在缓冲区溢出漏洞都可以导致远程代码执行,所以对于目前的IIS 6.0用户而言,可用的变通方案就是关闭WebDAV服务. 漏洞编号CVE-2017-7269 ...
- CSON vs JSON
CSON vs JSON 今天在github浏览资料时,无意发现了这个很像json,却优于json的cson.故,再次分享给大家! 官方fork文档:https://github.com/xgqfrm ...
- web online code editor All In One
web online code editor All In One 在线代码编辑器 Monaco Editor 摩纳哥编辑器 ️ 22.1k The Monaco Editor is the code ...
- C# 6.0 Features , C# 7.0 Features
1 1 1 C# 6.0 Features http://stackoverflow.com/documentation/c%23/24/c-sharp-6-0-features#t=20160828 ...
- free food icons
free food icons food icons Chinese foods https://www.flaticon.com/categories/food-and-restaurant htt ...