芹菜工作者之间共享XMPP连接 [英] Shared XMPP connection between Celery workers

查看:153
本文介绍了芹菜工作者之间共享XMPP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络应用程序需要能够发送XMPP消息(Facebook聊天),我认为Celery可能是一个很好的解决方案。一个任务包括查询数据库并将XMPP消息发送给多个用户。然而,使用这种方法,我每次运行一个任务时都必须连接到XMPP服务器,这不是一个好主意。

My web app needs to be able to send XMPP messages (Facebook Chat), and I thought Celery might be a good solution for this. A task would consist of querying the database and sending the XMPP message to a number of users. 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.

Facebook Chat API文档


最佳实践


  • 您的Facebook聊天整合应该仅用于预期会长时间的会话,住了。客户不应该快速地开启和关闭。

有没有办法在XMPP之间共享XMPP连接工作人员,所以我不需要重新连接每次我想发送一个消息?或者,是否有更好的解决方案?

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? 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. 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连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆