指定将数据存储在Rabbitmq消息队列中的格式

发布于 2025-02-07 08:46:35 字数 1047 浏览 2 评论 0 原文

我将RabbitMQ用作消息经纪和芹菜作为任务队列来处理我的队列内容。让我们以一个基本示例,要添加两个数字x和y。

我已经创建了共享任务为:

tasks.py

from celery import shared_task

@shared_task
def add(x, y):
    return x + y

我看到我将内容推到队列时,将数据存储为

”

(myprojectenv) root@ubuntu-s-1vcpu-1gb-blr1-02:/etc/myproject# python manage.py shell
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from myproject.tasks import add
>>> add.delay(5, 5)
<AsyncResult: 88f4d5c2-f68a-42c1-acda-d64593df1899>

但我希望我的数据以不同的格式存储,例如

{operation : 'add', listOfNumbers : [5, 5]}

如何更改方式,我的数据实际上被推入队列?在获取相同的同时,我可以得到字典并解开值并处理相同的处理

I am using RabbitMQ as message broker and Celery as task queue to process my queue content. Lets take a basic example where we want to add two numbers x and y.

I have created the shared task as :

tasks.py

from celery import shared_task

@shared_task
def add(x, y):
    return x + y

I see when I am pushing the content to the queue, the data is stored as

[[5, 5], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]

(myprojectenv) root@ubuntu-s-1vcpu-1gb-blr1-02:/etc/myproject# python manage.py shell
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from myproject.tasks import add
>>> add.delay(5, 5)
<AsyncResult: 88f4d5c2-f68a-42c1-acda-d64593df1899>

But instead I would like my data to be stored in a different format like

{operation : 'add', listOfNumbers : [5, 5]}

How can I change the way in which my data is actually getting pushed into the queue? While fetching the same I can get the dictionary and unpack the values and process the same

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2025-02-14 08:46:35
{
    "task": "myapp.tasks.add",
    "id": "54086c5e-6193-4575-8308-dbab76798756",
    "args": [4, 4],
    "kwargs": {}
}
{
    "task": "myapp.tasks.add",
    "id": "54086c5e-6193-4575-8308-dbab76798756",
    "args": [4, 4],
    "kwargs": {}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文