Celery是由Python开发的一个简单、灵活、可靠的处理大量任务的分发系统,它不仅支持实时处理也支持任务调度。

  • user:用户程序,用于告知celery去执行一个任务。
  • broker: 存放任务(依赖RabbitMQ或Redis,进行存储)
  • worker:执行任务

celery需要rabbitMQ、Redis、Amazon SQS、Zookeeper(测试中) 充当broker来进行消息的接收,并且也支持多个broker和worker来实现高可用和分布式。http://docs.celeryproject.org/en/latest/getting-started/brokers/index.html

    Celery version 4.0 runs on
Python ❨2.7, 3.4, 3.5❩
PyPy ❨5.4, 5.5❩
This is the last version to support Python 2.7, and from the next version (Celery 5.x) Python 3.5 or newer is required.
If you’re running an older version of Python, you need to be running an older version of Celery:

    Python </span>2.6: Celery series 3.1 <span style="color: #0000ff;">or</span><span style="color: #000000;"> earlier.
Python </span>2.5: Celery series 3.0 <span style="color: #0000ff;">or</span><span style="color: #000000;"> earlier.
Python </span>2.4 was Celery series 2.2 <span style="color: #0000ff;">or</span><span style="color: #000000;"> earlier. Celery </span><span style="color: #0000ff;">is</span> a project with minimal funding, so we don&rsquo;t support Microsoft Windows. Please don&rsquo;t open any issues related to that platform.</pre>

版本和要求

环境准备:

  • 安装rabbitMQ或Redis
        见:http://www.cnblogs.com/wupeiqi/articles/5132791.html
  • 安装celery
         pip3 install celery

快速上手

import time
from celery import Celery app = Celery('tasks', broker='redis://192.168.10.48:6379', backend='redis://192.168.10.48:6379') @app.task

def xxxxxx(x, y):

time.sleep(10)

return x + y

s1.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from s1 import xxxxxx # 立即告知celery去执行xxxxxx任务,并传入两个参数

result = xxxxxx.delay(4, 4)

print(result.id)

s2.py

from celery.result import AsyncResult
from s1 import app async = AsyncResult(id="f0b41e83-99cf-469f-9eff-74c8dd600002", app=app) if async.successful():

result = async.get()

print(result)

# result.forget() # 将结果删除

elif async.failed():

print('执行失败')

elif async.status == 'PENDING':

print('任务等待中被执行')

elif async.status == 'RETRY':

print('任务异常后正在重试')

elif async.status == 'STARTED':

print('任务已经开始被执行')

s3.py

执行 s1.py 创建worker(终端执行命令):

celery worker -A s1 -l info

执行 s2.py ,创建一个任务并获取任务ID:

python3 s2.py 

执行 s3.py ,检查任务状态并获取结果:

python3 s3.py

多任务结构

pro_cel
├── celery_tasks# celery相关文件夹
│   ├── celery.py # celery连接和配置相关文件
│   └── tasks.py # 所有任务函数
├── check_result.py # 检查结果
└── send_task.py # 触发任务
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from celery import Celery celery = Celery('xxxxxx',

broker='redis://192.168.0.111:6379',

backend='redis://192.168.0.111:6379',

include=['celery_tasks.tasks']) # 时区

celery.conf.timezone = 'Asia/Shanghai'

# 是否使用UTC

celery.conf.enable_utc = False

pro_cel/celery_tasks/celery

#!/usr/bin/env python
# -*- coding:utf-8 -*- import time

from .celery import celery @celery.task

def xxxxx(*args, **kwargs):

time.sleep(5)

return "任务结果" @celery.task

def hhhhhh(*args, **kwargs):

time.sleep(5)

return "任务结果"

pro_cel/celery_tasks/tasks.py

#!/usr/bin/env python
# -*- coding:utf-8 -*- from celery.result import AsyncResult

from celery_tasks.celery import celery async = AsyncResult(id="ed88fa52-11ea-4873-b883-b6e0f00f3ef3", app=celery) if async.successful():

result = async.get()

print(result)

# result.forget() # 将结果删除

elif async.failed():

print('执行失败')

elif async.status == 'PENDING':

print('任务等待中被执行')

elif async.status == 'RETRY':

print('任务异常后正在重试')

elif async.status == 'STARTED':

print('任务已经开始被执行')

pro_cel/check_result.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import celery_tasks.tasks # 立即告知celery去执行xxxxxx任务,并传入两个参数

result = celery_tasks.tasks.xxxxx.delay(4, 4) print(result.id)

pro_cel/send_task.py

更多配置:http://docs.celeryproject.org/en/latest/userguide/configuration.html

定时任务

1. 设定时间让celery执行一个任务

import datetime
from celery_tasks.tasks import xxxxx
"""
from datetime import datetime v1 = datetime(2017, 4, 11, 3, 0, 0)

print(v1) v2 = datetime.utcfromtimestamp(v1.timestamp())

print(v2) """

ctime = datetime.datetime.now()

utc_ctime = datetime.datetime.utcfromtimestamp(ctime.timestamp()) s10 = datetime.timedelta(seconds=10)

ctime_x = utc_ctime + s10

使用apply_async并设定时间

result = xxxxx.apply_async(args=[1, 3], eta=ctime_x)

print(result.id)

2. 类似于contab的定时任务

"""
celery beat -A proj
celery worker -A proj -l info """

from celery import Celery

from celery.schedules import crontab app = Celery('tasks', broker='amqp://47.98.134.86:5672', backend='amqp://47.98.134.86:5672', include=['proj.s1', ])

app.conf.timezone = 'Asia/Shanghai'

app.conf.enable_utc = False app.conf.beat_schedule = {

# 'add-every-10-seconds': {

# 'task': 'proj.s1.add1',

# 'schedule': 10.0,

# 'args': (16, 16)

# },

'add-every-12-seconds': {

'task': 'proj.s1.add1',

'schedule': crontab(minute=42, hour=8, day_of_month=11, month_of_year=4),

'args': (16, 16)

},

}

注:如果想要定时执行类似于crontab的任务,需要定制 Scheduler来完成。

Flask中应用Celery

pro_flask_celery/
├── app.py
├── celery_tasks
   ├── celery.py
    └── tasks.py
#!/usr/bin/env python
# -*- coding:utf-8 -*- from flask import Flask

from celery.result import AsyncResult from celery_tasks import tasks

from celery_tasks.celery import celery app = Flask(name) TASK_ID = None @app.route('/')

def index():

global TASK_ID

result = tasks.xxxxx.delay()

# result = tasks.task.apply_async(args=[1, 3], eta=datetime(2018, 5, 19, 1, 24, 0))

TASK_ID = result.id
</span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">任务已经提交</span><span style="color: #800000;">"</span><span style="color: #000000;">

@app.route('/result')

def result():

global TASK_ID

result = AsyncResult(id=TASK_ID, app=celery)

if result.ready():

return result.get()

return "xxxx"

if name == 'main':

app.run()

app.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from celery import Celery
from celery.schedules import crontab celery = Celery('xxxxxx',

broker='redis://192.168.10.48:6379',

backend='redis://192.168.10.48:6379',

include=['celery_tasks.tasks']) # 时区

celery.conf.timezone = 'Asia/Shanghai'

# 是否使用UTC

celery.conf.enable_utc = False

celery_tasks/celery.py

#!/usr/bin/env python
# -*- coding:utf-8 -*- import time

from .celery import celery @celery.task

def hello(*args, **kwargs):

print('执行hello')

return "hello" @celery.task

def xxxxx(*args, **kwargs):

print('执行xxxxx')

return "xxxxx" @celery.task

def hhhhhh(*args, **kwargs):

time.sleep(5)

return "任务结果"

celery_task/tasks.py

Django中应用Celery

一、基本使用

django_celery_demo
├── app01
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── tasks.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── django_celery_demo
│   ├── __init__.py
│   ├── celery.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── red.py
└── templates
#!/usr/bin/env python
# -*- coding:utf-8 -*- import os

from celery import Celery # set the default Django settings module for the 'celery' program.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery_demo.settings') app = Celery('django_celery_demo') # Using a string here means the worker doesn't have to serialize

the configuration object to child processes.

- namespace='CELERY' means all celery-related configuration keys

should have a CELERY_ prefix.

app.config_from_object('django.conf:settings', namespace='CELERY') # Load task modules from all registered Django app configs.

app.autodiscover_tasks()

django_celery_demo/celery.py

from .celery import app as celery_app

all = ('celery_app',)

django_celery_demo/__init__.py

from celery import shared_task

@shared_task

def add(x, y):

return x + y @shared_task

def mul(x, y):

return x * y @shared_task

def xsum(numbers):

return sum(numbers)

app01/tasks.py

...
....
.....
# ######################## Celery配置 ########################
CELERY_BROKER_URL = 'redis://10.211.55.20:6379'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_BACKEND = 'redis://10.211.55.20:6379'
CELERY_TASK_SERIALIZER = 'json'

django_celery_demo/settings.py

from django.shortcuts import render, HttpResponse
from app01 import tasks
from django_celery_demo import celery_app
from celery.result import AsyncResult def index(request):

result = tasks.add.delay(1, 8)

print(result)

return HttpResponse('...') def check(request):

task_id = request.GET.get('task')

async = AsyncResult(id=task_id, app=celery_app)

if async.successful():

data = async.get()

print('成功', data)

else:

print('任务等待中被执行')
</span><span style="color: #0000ff;">return</span> HttpResponse(<span style="color: #800000;">'</span><span style="color: #800000;">...</span><span style="color: #800000;">'</span>)</pre>

app01/views.py

"""django_celery_demo URL Configuration

The urlpatterns list routes URLs to views. For more information please see:

https://docs.djangoproject.com/en/1.11/topics/http/urls/

Examples:

Function views

1. Add an import: from my_app import views

2. Add a URL to urlpatterns: url(r'^\(', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^\)', Home.as_view(), name='home')

Including another URLconf

1. Import the include() function: from django.conf.urls import url, include

2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))

"""

from django.conf.urls import url

from django.contrib import admin

from app01 import views urlpatterns = [

url(r'^admin/', admin.site.urls),

url(r'^index/', views.index),

url(r'^check/', views.check),

]

django_celery_demo/urls.py

二、定时任务

1. 安装

install django-celery-beat

2. 注册app

INSTALLED_APPS = (
...,
'django_celery_beat',
)

3. 数据库去迁移生成定时任务相关表

python manage.py migrate

4. 设置定时任务

  • 方式一:代码中配置

    #!/usr/bin/env python
    # -*- coding:utf-8 -*- import os

    from celery import Celery # set the default Django settings module for the 'celery' program.

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery_demo.settings') app = Celery('django_celery_demo') # Using a string here means the worker doesn't have to serialize

    the configuration object to child processes.

    - namespace='CELERY' means all celery-related configuration keys

    should have a CELERY_ prefix.

    app.config_from_object('django.conf:settings', namespace='CELERY') app.conf.beat_schedule = {

    'add-every-5-seconds': {

    'task': 'app01.tasks.add',

    'schedule': 5.0,

    'args': (16, 16)

    },

    } # Load task modules from all registered Django app configs.

    app.autodiscover_tasks()

    django_celery_demo/celery.py

  • 方式二:数据表录入

5. 后台进程创建任务

celery -A django_celery_demo beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler

6. 启动worker执行任务

celery -A django_celery_demo worker -l INFO  

官方参考:http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#using-celery-with-django

python celery任务分发的更多相关文章

  1. python celery + redis

    redis http://debugo.com/python-redis celery http://docs.jinkan.org/docs/celery/getting-started/intro ...

  2. python celery多worker、多队列、定时任务

    python celery多worker.多队列.定时任务  

  3. python—Celery异步分布式

    python—Celery异步分布式 Celery  是一个python开发的异步分布式任务调度模块,是一个消息传输的中间件,可以理解为一个邮箱,每当应用程序调用celery的异步任务时,会向brok ...

  4. Python 库打包分发、setup.py 编写、混合 C 扩展打包的简易指南(转载)

    转载自:http://blog.konghy.cn/2018/04/29/setup-dot-py/ Python 有非常丰富的第三方库可以使用,很多开发者会向 pypi 上提交自己的 Python ...

  5. Python Celery队列

    Celery队列简介: Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery. 使用 ...

  6. python BitTornado P2P分发大文件

    P2P分发大文件思路 1.将软件包生成种子文件 2.通过saltstack将种子文件分发至每台服务器 3.每台服务器进行种子下载 推荐使用Twitter开源的murder.Twitter用它来分发大文 ...

  7. python celery 多work多队列

    1.Celery模块调用 既然celery是一个分布式的任务调度模块,那么celery是如何和分布式挂钩呢,celery可以支持多台不通的计算机执行不同的任务或者相同的任务. 如果要说celery的分 ...

  8. Python—Celery 框架使用

    一.Celery 核心模块 1. Brokers brokers 中文意思为中间人,在这里就是指任务队列本身,接收生产者发来的消息即Task,将任务存入队列.任务的消费者是Worker,Brokers ...

  9. python celery 异步学习

    1.运行redis 2.安装celery:pip install celery[redis] 3.vim task.py import time from celery import Celery b ...

随机推荐

  1. 《通过C#学Proto.Actor模型》之Spawning

    Props是配置Actor和实例化Actor,那实例化后,就应该访问了,Props.Actor提供了Actor.Spawn(),Actor.SpawnPrefix(),Actor.SpawnNamed ...

  2. Linux内存管理 (19)总结内存管理数据结构和API

    专题:Linux内存管理专题 关键词:mm.vaddr.VMA.page.pfn.pte.paddr.pg_data.zone.mem_map[]. 1. 内存管理数据结构的关系图 在大部分Linux ...

  3. LOJ3053 十二省联考2019 希望 容斥、树形DP、长链剖分

    传送门 官方题解其实讲的挺清楚了,就是锅有点多-- 一些有启发性的部分分 L=N 一个经典(反正我是不会)的容斥:最后的答案=对于每个点能够以它作为集合点的方案数-对于每条边能够以其两个端点作为集合点 ...

  4. iOS开发基础-图片切换(1)

    一.程序功能分析 1)点击左右箭头切换图片.序号.描述: 2)如果是首张图片,左边箭头失效: 3)如果是最后一张图片,右边箭头失效. 二.程序实现 定义确定图片位置.大小的常量: //ViewCont ...

  5. Java实现动态修改Jar包内文件内容

    import java.io.*; import java.util.Enumeration; import java.util.LinkedList; import java.util.List; ...

  6. CI/CD持续集成/持续部署 敏捷开发

    敏捷软件开发(英语:Agile software development),又称敏捷开发,是一种从1990年代开始逐渐引起广泛关注的一些新型软件开发方法,是一种应对快速变化的需求的一种软件开发能力.它 ...

  7. MySQL数据类型的选择

    +++++++++++++++++++++++++++++++++++++++++++标题:MySQL数据类型的选择时间:2019年2月22日内容:MySQL数据类型的选择范式参考重点:主要讲述MyS ...

  8. 数据库和SQL面试题基础知识(持续更新)

    数据库方面基础知识复习 常问小问题: 一.like查询大小写问题: sql查询结果去重 SELECT distinct name FROM 表:平均数avg 一.like查询大小写问题: ①用bina ...

  9. 解决Jenkins中执行jmeter脚本后不能发报告(原报告被覆盖、新报告无法保存)的问题

    我没有找到根本原因,但是我用了个取巧的办法: 先将原来的报告移到别的文件夹,执行完jmeter脚本后,再把那些旧报告移回来(也可以不移回来,我这里是为了能从jenkins页面上看).

  10. python抓取NBA现役球员基本信息数据并进行分析

    链接:http://china.nba.com/playerindex/ 所需获取JSON数据页面链接:http://china.nba.com/static/data/league/playerli ...