Django 中消息队列消费者放在哪里?
我在 Django 项目中使用 Carrot 作为消息队列,并遵循教程,而且效果很好。 但该示例在控制台中运行,我想知道如何在 Django 中应用它。 我从 models.py 中的模型之一调用的发布者类,所以没关系。 但我不知道将消费者类别放在哪里。
因为它只是与 .wait() 一起坐在那里,所以我不知道在什么时候或哪里需要实例化它,以便它始终运行并侦听消息!
谢谢!
I'm using Carrot for a message queue in a Django project and followed the tutorial, and it works fine. But the example runs in the console, and I'm wondering how I apply this in Django. The publisher class I'm calling from one of my models in models.py, so that's OK. But I have no idea where to put the consumer class.
Since it just sits there with .wait(), I don't know at what point or where I need to instantiate it so that it's always running and listening for messages!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您从教程中引用的示例中,消费者只是一个长时间运行的脚本。 它从队列中弹出一条消息,执行某些操作,然后调用 wait,并实质上进入休眠状态,直到另一条消息到来。
该脚本可以在您的帐户下的控制台上运行,也可以配置为 unix 守护程序或 win32 服务。 在生产中,您需要确保如果它死了,它可以重新启动,等等(守护进程或服务在这里更合适)。
或者您可以取出等待调用并在 Windows 调度程序下运行它或作为 cron 作业运行。 因此它每隔 n 分钟或某时间处理一次队列并退出。 这实际上取决于您的应用程序要求、您的队列填满的速度等。
这有意义还是我完全错过了您的要求?
The consumer is simply a long running script in the example you cite from the tutorial. It pops a message from the queue, does something, then calls wait and essentially goes to sleep until another message comes in.
This script could just be running at the console under your account or configured as a unix daemon or a win32 service. In production, you'd want to make sure that if it dies, it can be restarted, etc (a daemon or service would be more appropriate here).
Or you could take out the wait call and run it under the windows scheduler or as a cron job. So it processes the queue every n minutes or something and exits. It really depends on your application requirements, how fast your queue is filling up, etc.
Does that make sense or have I totally missed what you were asking?
如果您正在做的是处理任务,请查看 celery: http://github.com/ask/celery /
If what you are doing is processing tasks, please check out celery: http://github.com/ask/celery/