简体   繁体   English

Celery工作者之间的共享XMPP连接

[英]Shared XMPP connection between Celery workers

My web app needs to be able to send XMPP messages (Facebook Chat), and I thought Celery might be a good solution for this. 我的网络应用程序需要能够发送XMPP消息(Facebook聊天),我认为Celery可能是一个很好的解决方案。 A task would consist of querying the database and sending the XMPP message to a number of users. 任务包括查询数据库并将XMPP消息发送给许多用户。 However, with that approach I would have to connect to the XMPP server every time I run a task, which is not a great idea. 但是,使用这种方法,每次运行任务时都必须连接到XMPP服务器,这不是一个好主意。

From the Facebook Chat API docs : 来自Facebook Chat API文档

Best Practices 最佳实践

  • Your Facebook Chat integration should only be used for sessions that are expected to be long-lived. 您的Facebook聊天集成应仅用于预期长期存在的会话。 Clients should not rapidly churn on and off. 客户不应该快速流失。

Is there a way to share an XMPP connection between workers so I don't have to reconnect every time I want to send a message? 有没有办法在工作人员之间共享XMPP连接,所以每次我想发送消息时都不需要重新连接? Or, is there a better solution? 或者,有更好的解决方案吗?

You can create a connection globally in your celery task module and use it from your tasks to send messages. 您可以在celery任务模块中全局创建连接,并从任务中使用它来发送消息。 In that case the connection will be established at start-up and will be shared between worker processes. 在这种情况下,连接将在启动时建立,并将在工作进程之间共享。

import socket 
from celery.task import task

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect(('localhost', 9999)) 

@task
def echo(arg):
    s.send(arg) 
    return s.recv()

如何长时间运行后台作业,它的工作是从其他短期进程接收消息并将它们推送到XMPP套接字上?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM