我正在尝试使用 EC2 实例上的 Redis 作为代理运行一个简单的 Celery 任务,并从我的本地计算机连接到它,但是当 Celery 工作人员尝试执行该任务时我遇到了错误。报错信息如下:
The full contents of the message body was:
b'[[4, 4], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (81b)
The full contents of the message headers:
{'lang': 'py', 'task': 'tasks.add', 'id': '8f2b3e82-8a3b-4ee3-9038-574931e0022b', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None], 'root_id': '8f2b3e82-8a3b-4ee3-9038-574931e0022b', 'parent_id': None, 'argsrepr': '(4, 4)', 'kwargsrepr': '{}', 'origin': '[email protected]', 'ignore_result': False}
The delivery info for this task is:
{'exchange': '', 'routing_key': 'celery'}
Traceback (most recent call last):
File ".../celery/worker/consumer/consumer.py", line 591, in on_task_received
strategy = strategies[type_]
KeyError: 'tasks.add'
我在名为 tasks.py 的文件中定义了任务:
# tasks.py
from celery_config import app
from save_dataframe import save_dataframe
import pandas as pd
@app.task
def add(x, y):
return x + y
我的celery_config.py文件如下:
from celery import Celery
broker_url = 'redis://:[email protected]:6379/0'
result_backend = 'redis://:[email protected]:6379/0'
app = Celery('tasks', broker=broker_url, backend=result_backend)
我使用命令 celery -A tasks worker --loglevel=info 启动 Celery worker。当我运行测试脚本时,收到上述错误消息。我已经仔细检查了我的任务定义和 Celery 配置,但我仍然无法解决问题。
这里我附上了启动worker时的输出:
celery -A celery_config worker --loglevel=info
-------------- [email protected] v5.2.7 (dawn-chorus)
--- ***** -----
-- ******* ---- Linux-3.10.0-1127.18.2.el7.x86_64-x86_64-with-glibc2.17 2023-03-26 10:42:17
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x7f709e0066b0
- ** ---------- .> transport: redis://:**@ec2-35-89-171-51.us-west-2.compute.amazonaws.com:6379/0
- ** ---------- .> results: redis://:**@ec2-35-89-171-51.us-west-2.compute.amazonaws.com:6379/0
- *** --- * --- .> concurrency: 28 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
[2023-03-26 10:42:18,717: INFO/MainProcess] Connected to redis://...compute.amazonaws.com:6379/0
[2023-03-26 10:42:18,788: INFO/MainProcess] mingle: searching for neighbors
[2023-03-26 10:42:20,117: INFO/MainProcess] mingle: all alone
[2023-03-26 10:42:20,561: INFO/MainProcess] celery@... ready.
如果您能帮助识别和解决问题,我们将不胜感激!