celery使用

可以参考别人的博客使用

celery使用

1.项目布局

proj
 |____  __init__.py
 |____  celery.py
 |____  tasks.py
    
 '''
 	pip install celery==5.2.3(最好别用最新版本,目前最新5.2.7)
    pip install django-redis
    # Windows中还需要安装以下模块,用于任务执行单元
    pip install eventlet
 '''

在项目的配置文件中配置redis:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}
            # "PASSWORD": "123",
        }
    }
}

celery.py

from celery import Celery

app = Celery('proj',
             broker='amqp://',
             backend='rpc://',
             # broker='redis://:@127.0.0.1:6379/0'
             # backend='redis://:@127.0.0.1:6379/0'
             include=['proj.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    result_expires=3600,
)

if __name__ == '__main__':
    app.start()
'''
	The broker argument specifies the URL of the broker to use.
	该broker参数指定要使用的代理的 URL。

    The backend argument specifies the result backend to use.
	该backend参数指定要使用的结果后端。
    It’s used to keep track of task state and results. While results are disabled by default I use the RPC result backend here because I demonstrate how retrieving results work later. You may want to use a different backend for your application. They all have different strengths and weaknesses. If you don’t need results, it’s better to disable them. Results can also be disabled for individual tasks by setting the @task(ignore_result=True) option.
	它用于跟踪任务状态和结果。虽然默认情况下禁用结果,但我在此处使用 RPC 结果后端,因为我将演示稍后如何检索结果。您可能希望为您的应用程序使用不同的后端。他们都有不同的优点和缺点。如果您不需要结果,最好禁用它们。也可以通过设置@task(ignore_result=True)选项来禁用单个任务的结果。

    The include argument is a list of modules to import when the worker starts. You need to add our tasks module here so that the worker is able to find our tasks.	
	include参数是工作程序启动时要导入的模块列表。您需要在此处添加我们的任务模块,以便工作人员能够找到我们的任务。
'''

tasks.py

from .celery import app


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


@app.task
def mul(x, y):
    return x * y


@app.task
def xsum(numbers):
    return sum(numbe

启动工人

The celery program can be used to start the worker (you need to run the worker in the directory above proj):
celery程序可用于启动worker(您需要在proj上一级的目录中运行worker):


celery -A proj worker -l INFO

windows下执行
celery -A proj worker -l info -P eventlet

成功实例

如遇到报错,参考我的另一篇博客

配置项说明

配置项说明:

Celery 是通过配置文件中的配置项来定制任务的。

CELERY_IMPORTS: 配置导入哥哥任务的代码模块

CELERY_QUEUES: 定义任务执行的各个任务队列(如按照执行时间分slow、fast等),默认有一个队列,暂称为一般任务队列。

CELERY_ROUTES: 配置各个任务分配到不同的任务队列

CELERY_SCHEDULE: 配置各个任务执行的时机参数

 

CELERY_TIMEZONE: 设置时区

CELERY_ENABLE_UTC: 是否启动时区设置,默认值是True

CELERY_CONCURRENCY: 并发的worker数量

CELERY_PREFETCH_MULTIPLIER: 每次去消息队列读取任务的数量,默认值是4

CELERY_MAX_TASKS_PRE_CHILD: 每个worker执行多少次任务后会死掉

BROKER_URL: 消息队列   ******

CELERY_TASK_RESULT_EXPIRES: 任务执行结果的超时时间

CELERY_TASK_TIME_LIMIT: 单个任务运行的时间限制,超时会被杀死,不建议使用该参数,而用CELERY_TASK_SOFT_TIME_LIMIT

CELERY_RESULT_TACKEND: 结果存储  ******

CELERY_TASK_SERIALIZER: 任务序列化方式

CELERY_RESULT_SERIALIZER: 任务执行结果序列化方式

CELERY_DISABLE_RATE_LIMITS: 关闭执行限速
posted @ 2022-11-15 15:51  春游去动物园  阅读(31)  评论(0编辑  收藏  举报