Django 中所有 Celery 工作线程/内存缓存的全局可访问对象

发布于 2024-08-26 23:57:52 字数 589 浏览 4 评论 0原文

我有相当标准的 Django+Rabbitmq+Celery 设置,有 1 个 Celery 任务和 5 个工作人员。

任务将相同(我简化了一点)大文件(~100MB)异步上传到许多远程PC。

一切都工作正常,但代价是使用大量内存,因为每个任务/工作人员都分别将该大文件加载到内存中。

我想做的是拥有某种缓存,可供所有任务访问,即仅加载文件一次。基于 locmem 的 Django 缓存将是完美的,但就像文档所说:“每个进程都有自己的私有缓存实例”,我需要所有工作人员都可以访问此缓存。

尝试使用 Celery 信号,如 #2129820,但这不是我需要的。

所以问题是:有没有一种方法可以在 Celery 中定义全局的东西(比如基于 dict 的类,我可以在其中加载文件或其他东西)。或者在这种情况下我可以使用 Django 技巧吗?

谢谢。

I have pretty standard Django+Rabbitmq+Celery setup with 1 Celery task and 5 workers.

Task uploads the same (I simplify a bit) big file (~100MB) asynchronously to a number of remote PCs.

All is working fine at the expense of using lots of memory, since every task/worker load that big file into memory separatelly.

What I would like to do is to have some kind of cache, accessible to all tasks, i.e. load the file only once. Django caching based on locmem would be perfect, but like documentation says: "each process will have its own private cache instance" and I need this cache accessible to all workers.

Tried to play with Celery signals like described in #2129820, but that's not what I need.

So the question is: is there a way I can define something global in Celery (like a class based on dict, where I could load the file or smth). Or is there a Django trick I could use in this situation ?

Thanks.

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

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

发布评论

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

评论(3

蓝眼睛不忧郁 2024-09-02 23:57:52

为什么不简单地从磁盘流式传输上传而不是将整个文件加载到内存中?

Why not simply stream the upload(s) from disk instead of loading the whole file in memory ?

扛起拖把扫天下 2024-09-02 23:57:52

在我看来,你需要的是 django 支持的 memcached。这样 Celery 中的每个任务都可以访问它。

It seems to me that what you need is memcached backed for django. That way each task in Celery will have access to it.

云雾 2024-09-02 23:57:52

也许您可以使用线程而不是进程来完成此特定任务。由于线程都共享相同的内存,因此您只需要内存中数据的一份副本,但仍然可以并行执行。
(这意味着不使用 Celery 来完成此任务)

Maybe you can use threads instead of processes for this particular task. Since threads all share the same memory, you only need one copy of the data in memory, but you still get parallel execution.
( this means not using Celery for this task )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文