IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> django中运行celery定时任务worker报Received unregistered task of type ‘tasks.send_sms‘ -> 正文阅读

[Python知识库]django中运行celery定时任务worker报Received unregistered task of type ‘tasks.send_sms‘

1. 问题现场

1.1 项目目录结构

项目背景:我的项目对异步没有需求,但是有很多工作依赖定时任务。
celery

  • main.py
import os
from celery import Celery

app = Celery('devops')

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'devops.settings')
app.config_from_object('logic_celery.config')
app.autodiscover_tasks(packages=['logic_celery.sms'])
  • config.py
from datetime import timedelta

broker_url = 'redis://127.0.0.1:6379/0'
result_backend = 'redis://127.0.0.1:6379/1'
beat_schedule = {
    'send-msg-per-5s': {
        'task': 'tasks.send_sms',
        'schedule': timedelta(seconds=5),
        'args': (2911, ),
    }
}

注意上面两个脚本里暗藏着两个坑。下面会讲到。

  • tasks.py
from logic_celery.main import app
import time
import logging
log = logging.getLogger("django")


@app.task  # name表示设置任务的名称,如果不填写,则默认使用函数名做为任务名
def send_sms(num):
    """发送短信"""
    print("向手机号%s发送短信成功!"%num)
    time.sleep(5)

    return "send_sms OK"

1.2 启动celery beat

PS C:\work\com\code\data-analysis\devops> celery -A logic_celery.main beat -l info

输出:

celery beat v5.1.2 (sun-harmonics) is starting.
__    -    ... __   -        _
LocalTime -> 2022-05-16 14:48:14
Configuration ->
    . broker -> redis://127.0.0.1:6379/0
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . maxinterval -> 5.00 minutes (300s)
[2022-05-16 14:48:14,141: INFO/MainProcess] beat: Starting...
[2022-05-16 14:48:14,224: INFO/MainProcess] Scheduler: Sending due task send-msg-per-5s (tasks.send_sms)
[2022-05-16 14:48:19,227: INFO/MainProcess] Scheduler: Sending due task send-msg-per-5s (tasks.send_sms)
[2022-05-16 14:48:24,235: INFO/MainProcess] Scheduler: Sending due task send-msg-per-5s (tasks.send_sms)
[2022-05-16 14:48:29,244: INFO/MainProcess] Scheduler: Sending due task send-msg-per-5s (tasks.send_sms)
[2022-05-16 14:48:34,252: INFO/MainProcess] Scheduler: Sending due task send-msg-per-5s (tasks.send_sms)

特别注意beat Scheduler输出:Scheduler: Sending due task send-msg-per-5s (tasks.send_sms),这个是解决的关键之处。

1.3 启动celery worker

因为我是在windows下开发的,所以worker启动命令要加上-P eventlet参数

PS C:\work\com\code\data-analysis\devops> celery -A logic_celery.main worker -l info -P eventlet

输出:

-- ******* ---- Windows-10-10.0.19041-SP0 2022-05-16 14:49:18
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         devops:0x2a5f619f2b0
- ** ---------- .> transport:   redis://127.0.0.1:6379/0
- ** ---------- .> results:     redis://127.0.0.1:6379/1
- *** --- * --- .> concurrency: 4 (eventlet)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery



[tasks]
  . logic_celery.sms.tasks.send_sms
  . logic_celery.sms.tasks.send_sms2

[2022-05-16 14:49:18,244: INFO/MainProcess] Connected to redis://127.0.0.1:6379/0
[2022-05-16 14:49:18,254: INFO/MainProcess] mingle: searching for neighbors
[2022-05-16 14:49:19,285: INFO/MainProcess] mingle: all alone
[2022-05-16 14:49:19,294: INFO/MainProcess] pidbox: Connected to redis://127.0.0.1:6379/0.
[2022-05-16 14:49:19,304: INFO/MainProcess] celery@DESKTOP-cjk ready.
[2022-05-16 14:49:19,525: ERROR/MainProcess] Received unregistered task of type 'tasks.send_sms'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

这里可以看到celery已经发现了我的定时任务,说明我的任务注册是没问题的。

[tasks]
  . logic_celery.sms.tasks.send_sms
  . logic_celery.sms.tasks.send_sms2

但是,为啥后面又会报这个错呢?

[2022-05-16 14:49:19,525: ERROR/MainProcess] Received unregistered task of type 'tasks.send_sms'.

答案就是task名称不匹配。观察beat的输出就能知道,beat发送的任务名称是config.py里配置的beat_schedule的task字段值(tasks.send_sms);而worker自动发现注册的任务名称是全称(logic_celery.sms.tasks.send_sms)。这两个对不上号,所以worker就报错KeyError,其实worker没错,beat也没错,只是这俩的信息不对口,牛唇不对马嘴。

2. 解决办法

  • 修改config.py中beat_schedule的task字段值,写全称。
beat_schedule = {
    'send-msg-per-5s': {
        'task': 'logic_celery.sms.tasks.send_sms',
        'schedule': timedelta(seconds=5),
        'args': (2911, ),
    }
}
  • 重启beat和worker
PS C:\work\com\code\data-analysis\devops> celery -A logic_celery.main beat -l info
celery beat v5.1.2 (sun-harmonics) is starting.
__    -    ... __   -        _
LocalTime -> 2022-05-16 14:51:00
Configuration ->
    . broker -> redis://127.0.0.1:6379/0
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 minutes (300s)
[2022-05-16 14:51:46,035: INFO/MainProcess] Scheduler: Sending due task send-msg-per-5s (logic_celery.sms.tasks.send_sms)
[2022-05-16 14:51:51,041: INFO/MainProcess] Scheduler: Sending due task send-msg-per-5s (logic_celery.sms.tasks.send_sms)

PS C:\work\com\code\data-analysis\devops> celery -A logic_celery.main worker -l info -P eventlet
--- ***** -----
-- ******* ---- Windows-10-10.0.19041-SP0 2022-05-16 14:49:18
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         devops:0x2a5f619f2b0
- ** ---------- .> transport:   redis://127.0.0.1:6379/0
- ** ---------- .> results:     redis://127.0.0.1:6379/1
- *** --- * --- .> concurrency: 4 (eventlet)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
[tasks]
  . logic_celery.sms.tasks.send_sms
  . logic_celery.sms.tasks.send_sms2

[2022-05-16 14:49:18,244: INFO/MainProcess] Connected to redis://127.0.0.1:6379/0
[2022-05-16 14:49:18,254: INFO/MainProcess] mingle: searching for neighbors
[2022-05-16 14:49:19,285: INFO/MainProcess] mingle: all alone
[2022-05-16 14:49:19,294: INFO/MainProcess] pidbox: Connected to redis://127.0.0.1:6379/0.
[2022-05-16 14:49:19,304: INFO/MainProcess] celery@DESKTOP-Jaquan ready.
[2022-05-16 14:51:01,075: INFO/MainProcess] Task logic_celery.sms.tasks.send_sms[39970550-eb78-49a6-a9db-4bf99bfd90ff] received
[2022-05-16 14:51:01,075: WARNING/MainProcess] 向手机号2911发送短信成功!
[2022-05-16 14:51:01,075: WARNING/MainProcess]

[2022-05-16 14:51:05,997: INFO/MainProcess] Task logic_celery.sms.tasks.send_sms[707fc868-d472-49f6-b5ea-0693c0fc37b0] received
[2022-05-16 14:51:05,997: WARNING/MainProcess] 向手机号2911发送短信成功!
[2022-05-16 14:51:05,997: WARNING/MainProcess]

[2022-05-16 14:51:06,078: INFO/MainProcess] Task logic_celery.sms.tasks.send_sms[39970550-eb78-49a6-a9db-4bf99bfd90ff] succeeded in 5.0s: 'send_sms OK'
[2022-05-16 14:51:11,003: INFO/MainProcess] Task logic_celery.sms.tasks.send_sms[707fc868-d472-49f6-b5ea-0693c0fc37b0] succeeded in 5.0s: 'send_sms OK'

完美解决!

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-05-18 17:35:34  更:2022-05-18 17:36:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 13:30:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码