kongluoxing / celerybeatredis

celery scheduler use redis
57 stars 42 forks source link

Would you give some code to read ? #4

Closed zanpen2000 closed 9 years ago

zanpen2000 commented 9 years ago

I don't understand how make it works, just need a sample code like tasks.py , please help me , thanks!

kongluoxing commented 9 years ago

It's just a Celery beat scheduler, so you may first look into celery doc: http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html If you already know that, then you'd understand this project is for storing CELERYBEAT_SCHEDULE to redis instead of in Celery config file, so that you can dynamically add/modify and refresh config in memory.

zanpen2000 commented 9 years ago

谢谢抽时间回复! 是的,我看过文档了,但当我做了一个练习,却无论如何不能使beat运行,很疑惑我是否写的正确,所以,我想要一份例子代码校验一下。 2015年9月1日 下午10:04,"Kong Luoxing" notifications@github.com写道:

It's just a Celery beat scheduler, so you may first look into celery doc: http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html If you already know that, then you'd understand this project is for storing CELERYBEAT_SCHEDULE to redis instead of in Celery config file, so that you can dynamically add/modify and refresh config in memory.

— Reply to this email directly or view it on GitHub https://github.com/kongluoxing/celerybeatredis/issues/4#issuecomment-136732670 .

kongluoxing commented 9 years ago

首先, 你是否了解celery的使用? 如果还不了解, 请移步celery文档: http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html

如果已经了解celery, 尝试用以下步骤启动:

  1. 更新celerybeat-redis `pip install -U celerybeat-redis
  2. 写一个测试的task eg. tasks.py
from celery import Celery

app = Celery('tasks')

@app.task
def add(x, y):
    return x + y
  1. 创建一个celery的配置文件
touch __init__.py # celery会import配置文件, 因此必须在一个package里
在celeryconfig.py中添加如下配置:
from datetime import timedelta

CELERY_REDIS_SCHEDULER_URL = "redis://localhost:6379/1"
BROKER_URL = "redis://localhost:6379"
CELERY_REDIS_SCHEDULER_KEY_PREFIX = 'tasks:meta:'
CELERYBEAT_SCHEDULE = {
        # Executes every Monday morning at 7:30 A.M
        'add-every-3-sec': {
                'task': 'tasks.add',
                'schedule': timedelta(seconds=3),
                'args': (16, 16),
        },
}

以上配置表示用redis做broker和任务信息的存储, 任务前缀是tasks:meta:, 这里添加了一个tasks.add的任务, 每三秒运行一次. 这种使用方式只用于测试用途, 正式使用还是直接在redis中添加任务, 详见README.

  1. 测试运行 启动redis-server 启动worker: celery -A tasks worker --loglevel=debug 启动beat: celery beat -S celerybeatredis.schedulers.RedisScheduler

观察worker的输出, 看到输出结果, 则表示beat正常运行了, 如果不行, 试一下直接用celery执行:

from tasks import add
add.delay(4, 4)
zanpen2000 commented 9 years ago

谢谢抽时间解答! 问题已经解决了,详见链接:https://github.com/zakird/celerybeat-mongo/issues/15

我按照例子写了一段代码,无论如何Beat无法自动运行,鉴于celerybeatredis和celerybeatmongo同宗同源,一时病急乱投医,加上问题问的含糊,想必也让你有困惑,罪过呀~

好在现在问题解决了,很感谢你能抽时间解答,祝工作愉快!

在 2015年9月5日 下午1:14,Kong Luoxing notifications@github.com写道:

首先, 你是否了解celery的使用? 如果还不了解, 请移步celery文档: http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html

如果已经了解celery, 尝试用以下步骤启动:

  1. 更新celerybeat-redis `pip install -U celerybeat-redis
  2. 写一个测试的task eg. tasks.py

from celery import Celery

app = Celery('tasks')

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

  1. 创建一个celery的配置文件

touch init.py # celery会import配置文件, 因此必须在一个package里 在celeryconfig.py中添加如下配置: from datetime import timedelta

CELERY_REDIS_SCHEDULER_URL = "redis://localhost:6379/1" BROKER_URL = "redis://localhost:6379" CELERY_REDIS_SCHEDULER_KEY_PREFIX = 'tasks:meta:' CELERYBEAT_SCHEDULE = {

Executes every Monday morning at 7:30 A.M

    'add-every-3-sec': {
            'task': 'tasks.add',
            'schedule': timedelta(seconds=3),
            'args': (16, 16),
    },

}

以上配置表示用redis做broker和任务信息的存储, 任务前缀是tasks:meta:, 这里添加了一个tasks.add的任务, 每三秒运行一次. 这种使用方式只用于测试用途, 正式使用还是直接在redis中添加任务, 详见README.

  1. 测试运行 启动redis-server 启动worker: celery -A tasks worker --loglevel=debug 启动beat: celery beat -S celerybeatredis.schedulers.RedisScheduler

观察worker的输出, 看到输出结果, 则表示beat正常运行了, 如果不行, 试一下直接用celery执行:

from tasks import add add.delay(4, 4)

— Reply to this email directly or view it on GitHub https://github.com/kongluoxing/celerybeatredis/issues/4#issuecomment-137911694 .