# celery 异步任务的调度和处理,耗时的任务异步执行,与 RabbitMQ、Redis、Amazon SQS 中间件集成 ## Usage ``` pip install celery from celery import Celery # 创建 Celery 应用 app = Celery('tasks', broker='redis://localhost:6379/0') # 定义一个 Celery 任务 @app.task def add(x, y): return x + y # 调用 Celery 任务,其中4,6为add函数的参数 result = add.delay(4, 6) # 等待任务执行完成并获取结果 print("Task ID:", result.id) print("Task Status:", result.status) print("Task Result:", result.get()) ``` 命令行中运行: ``` celery -A app.celery_app.worker.example worker -l info -Q example-queue -c 1 ``` -A 指定python模块 -l info 日志级别 -Q example-queue 指定worker从example-queue队列中取任务执行 -c 1 启动一个worker进程