celery

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

  1. 你想对100台机器执行一条批量命令,可能会花很长时间 ,但你不想让你的程序等着结果返回,而是给你返回 一个任务ID,你过一段时间只需要拿着这个任务id就可以拿到任务执行结果, 在任务执行ing进行时,你可以继续做其它的事情。 
  2. 你想做一个定时任务,比如每天检测一下你们所有客户的资料,如果发现今天 是客户的生日,就给他发个短信祝福

 

Celery 在执行任务时需要通过一个消息中间件来接收和发送任务消息,以及存储任务结果, 一般使用rabbitMQ or Redis,后面会讲

1.1 Celery有以下优点:

  1. 简单:一单熟悉了celery的工作流程后,配置和使用还是比较简单的
  2. 高可用:当任务执行失败或执行过程中发生连接中断,celery 会自动尝试重新执行任务
  3. 快速:一个单进程的celery每分钟可处理上百万个任务
  4. 灵活: 几乎celery的各个组件都可以被扩展及自定制

Celery基本工作流程图

 

 

1.2 Celery安装使用

Celery的默认broker是RabbitMQ, 仅需配置一行就可以

broker_url = 'amqp://guest:guest@localhost:5672//'

rabbitMQ 没装的话请装一下,安装看这里  http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html#id3

 

使用Redis做broker也可以

配置

Configuration is easy, just configure the location of your Redis database:

app.conf.broker_url = 'redis://localhost:6379/0'

Where the URL is in the format of:

redis://:password@hostname:port/db_number

all fields after the scheme are optional, and will default to localhost on port 6379, using database 0.

 

如果想获取每个任务的执行结果,还需要配置一下把任务结果存在哪

If you also want to store the state and return values of tasks in Redis, you should configure these settings:

app.conf.result_backend = 'redis://localhost:6379/0'

celery

1. 基本使用

"""
celery worker  -A tasks -l info

"""
import pytz
from celery import Celery

app = Celery('tasks', broker='amqp://47.98.134.86:5672', backend='amqp://47.98.134.86:5672')

@app.task
def add(x, y):
    print("running...", x, y)
    import time
    time.sleep(10)
    return x + y
s1.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from s1 import add

"""
# 执行任务
result = add.delay(4, 4)
print(result)

# 检查任务是否已经完成
print(result.ready())

# 获取任务结果:可以设置timeout超时
v = result.get()
print(v)
"""

result = add.delay(4, 4)
from celery.result import AsyncResult
print(result,type(result))

"""
from celery.task.control import revoke
revoke(id, terminate=True)
"""
s2.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from celery import Celery
from celery.result import AsyncResult

app = Celery('tasks', broker='amqp://47.98.134.86:5672', backend='amqp://47.98.134.86:5672')

result = AsyncResult(id="f380db43-8998-4fb0-b3a4-cd1cbd49f14e", app=app)
print(result.get())
s3.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from datetime import datetime
from s1 import add
from celery.result import AsyncResult

# result = add.apply_async(args=[1, 3], eta=datetime(2018, 4, 11, 2, 32, 0))

"""
from datetime import datetime

v1 = datetime(2017, 4, 11, 3, 0, 0)
print(v1)

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

"""
result = add.apply_async(args=[1, 3], eta=datetime(2018, 4, 11, 3, 0, 0))
print(type(result))
# result.revoke()
print(result.get())
s4.py

2. 多文件

part2
├── __pycache__
├── s2.py
└── tasks
    ├── __init__.py
    ├── celery.py
    └── s1.py

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

# app = Celery('tasks', broker='redis://192.168.0.100:6379/0', backend='redis://192.168.0.100:6379/0')
app = Celery('tasks', broker='amqp://47.98.134.86:5672', backend='amqp://47.98.134.86:5672', include=['tasks.s1'])

app.conf.update(
    result_expires=3600,
)
celery.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time
from .celery import app


@app.task
def add1(x, y):
    time.sleep(1)
    return x + y


@app.task
def add2(x, y):
    time.sleep(2)
    return x + y


@app.task
def add3(x, y):
    time.sleep(3)
    return x + y
s1.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tasks.s1 import add1


result = add1.delay(4, 4)
from celery.result import AsyncResult
print(result,type(result))
s2.py
celery worker -A tasks

celery multi start n1 -A task
celery multi stop  n1 -A task
celery multi stopwait  n1 -A task

3. 定时任务

a. 函数版本

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
启动定制任务:
    celery beat -A s1
    需要依赖celerybeat-schedule.db,所以要对文件夹有写的权限
    或 celery -A periodic_task beat -s /home/celery/var/run/celerybeat-schedule

执行:
    celery worker -A s1


"""
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')


@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # 每10s执行一次:test('hello')
    sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')

    # 每30s执行一次:test('world')
    sender.add_periodic_task(30.0, test.s('world'), expires=10)

    # 每天早上7:30执行一次:test('Happy Mondays!')
    sender.add_periodic_task(
        crontab(hour=7, minute=30, day_of_week=1),
        test.s('Happy Mondays!'),
    )

    # 每周3,5的3,7,20点 每12分钟执行一次:test('Happy Mondays!')
    sender.add_periodic_task(
        crontab(
            minute=12, hour="3,7,20", day_of_week='thu,fri', day_of_month="*", day_of_year='*',
        ),
        test.s('11111'),
    )

    # 每周3,5的3,7,20点 每12分钟执行一次:test('Happy Mondays!')
    sender.add_periodic_task(
        crontab(
            minute=25, hour=7, day_of_month=11, month_of_year=4,
        ),
        test.s('11111'),
    )


@app.task
def test(arg):
    print(arg)
函数版本

b. 配置版本

proj/
├── celery.py
└── s1.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
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)
    },
}
celery.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time
from .celery import app


@app.task
def add1(x, y):
    import datetime
    print(datetime.datetime.now())
    return x + y


@app.task
def add2(x, y):
    return x + y


@app.task
def add3(x, y):
    return x + y
s1.py

flask celery应用

proj
├── app.py
└── celery_tasks
    └── tasks.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
celery worker -A app.celery -l info
python3 app.py

"""
from flask import Flask
from celery import Celery
from celery.result import AsyncResult

app = Flask(__name__)

celery = Celery('xxxxxx', broker='amqp://47.98.134.86:5672', backend='amqp://47.98.134.86:5672',
                include=['celery_tasks.tasks'])

TASK_ID = None

from celery_tasks import tasks


@app.route('/')
def index():
    global TASK_ID
    result = tasks.task.delay()
    # result = tasks.task.apply_async(args=[1, 3], eta=datetime(2018, 4, 11, 1, 24, 0))
    TASK_ID = result.id

    return "xxxx"


@app.route('/ready')
def ready():
    global TASK_ID
    result = AsyncResult(id=TASK_ID, app=celery)
    return str(result.ready())


@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 app import celery


@celery.task
def task(*args, **kwargs):
    import time
    time.sleep(5)
    print('..........')
    return "任务结果"
task.py

 

 

 

posted @ 2017-08-03 18:02  武沛齐  阅读(...)  评论(... 编辑 收藏