我试图创建一个能够构造的类,并返回一个在某些时间点可调用的对象并向RabbitMQ发送消息
我正在使用Pika而我已经尝试过这段代码,但除非我打电话给ioloop但我不需要一个而且ioloop卡住我剩下的代码,但它不会启动叫这个班。
任何想法如何实现?
import pika
导入ConfigurationManager 导入json import gc
类MessageSender:
def __init__(self,brokerUrl,queueName):
credentials = pika.PlainCredentials('admin', 'admin')
self.brokerUrl = brokerUrl
self.queueName = queueName
self.channel = None
self.connectionParameters = pika.ConnectionParameters(host=self.brokerUrl, credentials=credentials)
self.connection = pika.SelectConnection(self.connectionParameters, on_open_callback=self.onOpen, custom_ioloop=None)
def onOpen(self, connection):
connection.channel(self.openChannel)
def openChannel(self, channel):
channel.queue_declare(None, queue=self.queueName, durable=True)
self.channel = channel
def createMessageJson(self,commandType,searchId,senderChannel,data,runId=None, originalMessage=None):
rtnDict = {}
rtnDict["commandType"] = commandType
rtnDict["searchId"] = searchId
rtnDict["receipientChannel"] = self.queueName
rtnDict["senderChannel"] = senderChannel
rtnDict["data"] = data
if 'executionPlanSchema' in data:
rtnDict['executionPlanSchema'] = data['executionPlanSchema']
if runId != None:
rtnDict["runId"] = runId
if originalMessage != None:
rtnDict["originalMessage"] = originalMessage
return rtnDict
#def sendMessage(self,msg):
# tornado.ioloop.IOLoop.current().run_sync(lambda: self.send(msg))
def sendMessageAsync(self,msg):
msg = json.dumps(msg)
self.channel.basic_publish(exchange='', routing_key=self.queueName, body=msg)
gc.collect()
def sendMessage(self,msg):
msg = json.dumps(msg)
self.channel.basic_publish(exchange='', routing_key=self.queueName, body=msg)
gc.collect()