celery使用的时候的坑

本文探讨了Celery中delay及apply_async函数的参数传递限制,特别是实例对象不可传递的问题,并深入分析了当使用Redis作为broker时,由于visibility_timeout设置不当导致的任务重复执行现象。提供了调整配置避免重复执行的解决方案。
一、delay函数或者apply_async函数的传参问题

1、通过delay或者apply_async传参数给异步任务的时候不能传实例,否则会报错raised unexpected: EncodeError(TypeError('<ExtractCashRecord: ExtractCashRecord object> is not JSON serializable,因为我传的参数有一个是ExtractCashRecord这个类的实例。

二、任务重复执行

celery使用redis作为broker的话,apply_async的异步任务如果执行之间是延迟超过1小时的话,celery会重复发布任务,会导致任务重复执行。这个在官方文档中有说明celery文档

if a task isn’t acknowledged within the Visibility Timeout the task will be redelivered to another worker and executed.This causes problems with ETA/countdown/retry tasks where the time to execute exceeds the visibility timeout; in fact if that happens it will be executed again, and again in a loop.So you have to increase the visibility timeout to match the time of the longest ETA you’re planning to use.Note that Celery will redeliver messages at worker shutdown, so having a long visibility timeout will only delay the redelivery of ‘lost’ tasks in the event of a power failure or forcefully terminated workers.Periodic tasks won’t be affected by the visibility timeout, as this is a concept separate from ETA/countdown.You can increase this timeout by configuring a transport option with the same name:
app.conf.broker_transport_options = {'visibility_timeout': 43200}*
The value must be an int describing the number of seconds.

这个时候,我们就需要在配置文件里面增加一项,来增大visibility_timeout的时间
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 43200}
或者
app.conf.broker_transport_options = {'visibility_timeout': 43200}

转载于:https://www.cnblogs.com/lanlingshao/p/10087280.html

### Celery 的基本使用方法 Celery 是一种用于处理分布式任务队列的强大工具,支持异步任务和定时任务。以下是关于如何设置并使用 Celery 的详细介绍。 #### 1. 安装依赖库 在开始之前,需要安装 Celery 和消息中间件(如 RabbitMQ 或 Redis)。可以通过 pip 命令完成安装: ```bash pip install celery ``` 如果选择 Redis 作为消息中间件,则还需要安装 `redis-py` 库: ```bash pip install redis ``` --- #### 2. 配置 Celery 应用 创建一个 Python 文件(例如 `tasks.py`),定义 Celery 应用及其配置: ```python from celery import Celery # 初始化 Celery 应用 app = Celery( 'my_task', broker='redis://127.0.0.1:6379/0', backend='redis://127.0.0.1:6379/0' ) @app.task def add(x, y): return x + y ``` 此代码片段展示了如何初始化 Celery 应用,并将其连接到 Redis 中间件[^4]。 --- #### 3. 启动 Celery Worker 启动 Celery worker 来监听任务请求。打开终端,在项目目录下运行以下命令: ```bash celery -A tasks worker --loglevel=info ``` 这会启动一个工作进程来接收来自 Broker 的任务请求[^1]。 --- #### 4. 执行任务 可以在另一个脚本中调用已注册的任务。例如: ```python from tasks import add result = add.delay(4, 6) print(f"Task ID: {result.id}") print(f"Result: {result.get()}") ``` 这里通过 `.delay()` 方法提交任务给 Celery 处理,并返回结果对象以便查询状态或获取最终计算结果[^3]。 --- #### 5. 设置定时任务 (Optional) 为了实现周期性调度功能,可以利用 Celery 提供的 Beat 功能。首先修改配置文件以包含时间表选项: ```python CELERY_BEAT_SCHEDULE = { 'add-every-30-seconds': { 'task': 'tasks.add', 'schedule': 30.0, 'args': (16, 16), }, } ``` 接着开启 beat 服务端口: ```bash celery -A tasks beat --loglevel=info ``` 这样就可以按照设定的时间间隔自动触发指定的操作了[^5]。 --- ### 总结 以上就是有关于怎样去构建以及操作基础版别的 Celery 流程介绍。它主要涵盖了从环境搭建直至实际运用各个环节的知识要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值