celery

How to structure celery tasks

谁都会走 提交于 2020-01-12 03:07:35
问题 I have 2 types of task: async tasks and schedule tasks. So, here is my dir structure: proj | -- tasks | -- __init__.py | -- celeryapp.py => celery instance defined in this file. | -- celeryconfig.py | -- async | | | -- __init__.py | | | -- task1.py => from proj.tasks.celeryapp import celery | | | -- task2.py => from proj.tasks.celeryapp import celery | -- schedule | -- __init__.py | -- task1.py => from proj.tasks.celeryapp import celery | -- task2.py => from proj.tasks.celeryapp import celery

schedule与apscheduler与celery

谁说我不能喝 提交于 2020-01-11 16:50:21
定时任务: schedule与apscheduler与celery 量级: schedule < apscheduler < celery 三者都支持定时任务配置: -- schedule相当于linux下的crontab,使用最简单,但不支持动态添加任务和任务实例化,所以在实际项目中使用不多。 -- apschedule解决了schedule的不足,项目中定时任务使用最多 -- celery 的功能强点在异步队列,定时功能只是一个附加功能,如果只为定时而使用celery则太过笨重,杀鸡用牛刀。 想想定时任务需支持的几个关键点: 1.触发方式: 按照特定频率/特定时间 2.调度方法:阻塞/非阻塞/异步 3.任务的实例化,为什么实例化?具体使用看应用场景: -- 如果重启了一切重来,就按照默认的任务存内存,简单高效; -- 当程序崩溃或应用重启时,还需要保持定时任务的正常流程,比如(程序A每两个小时运行一次,10点运行了,11点应用重启,那么还会维持下次在12点执行,因为任务的调度已经实例化到数据库), 实例化的时候又要考虑,任务重复添加到数据库的问题,不急。有相关配置,是替换还是继续添加,追加就会存在同时有多个任务同时进行。 4.任务的执行,是采用多线程还是多进程?看你是CPU密集型还是IO密集型,如果是后者就没必要浪费进程数了,也可以二者结合使用。 当任务未执行完毕

Django Celery cache lock did not work?

[亡魂溺海] 提交于 2020-01-11 09:43:10
问题 I am trying to use Django cache to implement a lock mechanism. In Celery offical site, it claimed Django cache work fine for this. However, in my experence, it did not work. My experience is that if there are multiple threads/processes acquire the lock in almost the same time (close to ~0.003 second), all threads/processes will get the lock successfully. For other threads which acquire lock later than ~0.003 second, it fails. Am I the only person experienced this? Please correct me if

Celery的使用

此生再无相见时 提交于 2020-01-10 22:03:21
一、celery介绍 1. 什么是celery Celery是一个简单、灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 celery单独使用一个socket,不会额外占用其他程序的资源。 项目中使用celery的优势 减少服务器的压力 提供了3中任务的执行方式 2. celery架构 Celery的架构由三部分组成,消息中间件(message broker)、任务执行单元(worker)和 任务执行结果存储(task result store)组成。 celery的架构图 (1)消息中间件 celery本身不提供消息服务,但是可以和第三方提供的消息中间件集成。包括,RabbitMQ, Redis等等 (2)任务执行单元 worker是celery提供的任务执行单元,worker并发的运行在分布式系统节点中 (3)任务结果存储 Task result store用来存储worker执行的任务结果,celery支持以不同方式存储任务的结果,包括AMQP,redis等 3. celery的使用场景 celery中共提供了3中任务执行的方式,分别应用于3个场景 异步执行 延迟执行 定时执行 (1)异步执行 异步的执行分配的任务 (2)延迟执行 就是在之后的某一个时间点,执行该指定的任务 (3)定时执行 解决周期任务(就是周期的执行某一个任务) 4.

Celery的使用

别说谁变了你拦得住时间么 提交于 2020-01-10 20:03:27
目录 Celery的使用 Celery简介 官方链接 Celery架构 使用场景 Celery的安装配置 Celery执行异步任务 基本使用 高级使用 django中使用celery Celery的使用 Celery简介 Celery是一个简单、灵活且可靠的,处理大量消息的分布式系统,专注于实时处理的异步任务队列,同时也支持任务调度,可用于处理实时数据以及任务调度。 官方链接 Celery 官网:http://www.celeryproject.org/ Celery 官方文档英文版:http://docs.celeryproject.org/en/latest/index.html Celery 官方文档中文版:http://docs.jinkan.org/docs/celery/ Celery架构 Celery的架构由三部分组成,消息中间件(message broker)、任务执行单元(worker)和 任务执行结果存储(task result store)组成。 消息中间件 Celery本身不提供消息服务,但是可以方便的和第三方提供的消息中间件集成。包括,RabbitMQ, Redis等等 任务执行单元 Worker是Celery提供的任务执行的单元,worker并发的运行在分布式的系统节点中。 任务结果存储 Task result

Celery操作

拈花ヽ惹草 提交于 2020-01-10 19:19:20
Celery 官方 Celery 官网:http://www.celeryproject.org/ Celery 官方文档英文版:http://docs.celeryproject.org/en/latest/index.html Celery 官方文档中文版:http://docs.jinkan.org/docs/celery/ Celery架构 Celery的架构由三部分组成,消息中间件(message broker)、任务执行单元(worker)和 任务执行结果存储(task result store)组成。 消息中间件 Celery本身不提供消息服务,但是可以方便的和第三方提供的消息中间件集成。包括,RabbitMQ, Redis等等 任务执行单元 Worker是Celery提供的任务执行的单元,worker并发的运行在分布式的系统节点中。 任务结果存储 Task result store用来存储Worker执行的任务的结果,Celery支持以不同方式存储任务的结果,包括AMQP, redis等 使用场景 异步执行:解决耗时任务 延迟执行:解决延迟任务 定时执行:解决周期(周期)任务 Celery的安装配置 pip install celery 消息中间件:RabbitMQ/Redis app=Celery('任务名', broker='xxx', backend='xxx

Django Celery get task count

别说谁变了你拦得住时间么 提交于 2020-01-10 13:52:33
问题 I am currently using django with celery and everything works fine. However I want to be able to give the users an opportunity to cancel a task if the server is overloaded by checking how many tasks are currently scheduled. How can I achieve this ? I am using redis as broker. I just found this : Retrieve list of tasks in a queue in Celery It is somehow relate to my issue but I don't need to list the tasks , just count them :) 回答1: If your broker is configured as redis://localhost:6379/1 , and

Django Celery get task count

拟墨画扇 提交于 2020-01-10 13:52:30
问题 I am currently using django with celery and everything works fine. However I want to be able to give the users an opportunity to cancel a task if the server is overloaded by checking how many tasks are currently scheduled. How can I achieve this ? I am using redis as broker. I just found this : Retrieve list of tasks in a queue in Celery It is somehow relate to my issue but I don't need to list the tasks , just count them :) 回答1: If your broker is configured as redis://localhost:6379/1 , and

Django Celery get task count

怎甘沉沦 提交于 2020-01-10 13:52:12
问题 I am currently using django with celery and everything works fine. However I want to be able to give the users an opportunity to cancel a task if the server is overloaded by checking how many tasks are currently scheduled. How can I achieve this ? I am using redis as broker. I just found this : Retrieve list of tasks in a queue in Celery It is somehow relate to my issue but I don't need to list the tasks , just count them :) 回答1: If your broker is configured as redis://localhost:6379/1 , and

django使用celery异步发送短信

戏子无情 提交于 2020-01-07 23:36:11
1. 安装celery模块 pip install -U celery==4.3.0 2. 创建celery相关目录 xiaolan/ # 项目主目录 ├── mycelery/ ├── config.py # 配置文件 ├── __init__.py ├── main.py # 主程序 └── sms/ # 一个目录可以放置多个任务,该目录下存放当前任务执行时需要的模块或依赖 └── tasks.py # 任务的文件,名称必须是这个 3. 创建celery配置文件config.py # 注意: 我的redis因为没有设置密码所有可以这样用 # 任务队列的链接地址 broker_url = 'redis://127.0.0.1:6379/15' # 结果队列的链接地址 result_backend = 'redis://127.0.0.1:6379/14' 4. 在man.py主程序中对django的配置文件进行加载, 把django和celery进行组合 import os import django from celery import Celery # 主程序 # 创建celery实例对象 app = Celery("xiaolan") # 把celery和django进行结合, 识别和加载django的配置文件 os.environ.setdefault('DJANGO