简体   繁体   English

调用多个函数作为 RABBITMQ 消息

[英]Calling more than one functions as RABBITMQ message

I am just starting to use RabbitMQ and Python in general.我刚刚开始使用 RabbitMQ 和 Python。 I have been reading the tuts on the rabbit official page, but I have no idea how to use Rabbitmq to do another things.我一直在看rabbit官方页面上的tuts,但是我不知道如何使用Rabbitmq做其他事情。

I have been trying to run the example of this [tutorial] ( https://www.rabbitmq.com/tutorials/tutorial-three-python.html ), It runs well,.. BUT I need to know how can I create more than one fucntions and call them throug Rabbitmq Messages?... (I am also using this [example] ( Python and RabbitMQ - Best way to listen to consume events from multiple channels? ) to guide me. )我一直在尝试运行这个 [教程] ( https://www.rabbitmq.com/tutorials/tutorial-three-python.html ) 的示例,它运行良好,.. 但我需要知道如何创建多个功能并通过 Rabbitmq 消息调用它们?...(我也在使用这个 [示例]( Python 和 RabbitMQ - 聆听来自多个渠道的消费事件的最佳方式? )来指导我。)

I hope someone have some idea how to do this... (I will repeat again, I am very new on this topics)...我希望有人知道如何做到这一点......(我会再次重复,我对这个话题很陌生)......

This some code what i have.这是我所拥有的一些代码。

I use this code to send the message as the tutorial..我使用此代码作为教程发送消息..

import pika
import sys

url = 'amqp://oamogcgg:xxxxxxxxxxxxxxxxxxxxxxxxx@salamander.rmq.cloudamqp.com/oamogcgg'
params = pika.URLParameters(url)
connection = pika.BlockingConnection(params)
channel = connection.channel()


channel.exchange_declare(exchange='logs', exchange_type='fanout')

message = ' '.join(sys.argv[1:]) or "info: THIS IS A TEST MESSAGE !!!!!!"
channel.basic_publish(exchange='logs', routing_key='', body=message)
print(" [x] Sent %r" % message)
connection.close()

And in this file is where I receive the message according to me.在这个文件中是我根据我收到消息的地方。

import pika
import sys
import threading

threads=[]


#function 1 

def validator1(channel):    
channel.queue_declare(queue='queue_name')
print (' [*] Waiting messsaes for valiadtor1 press CTRL+C')


def callback(ch, method, properties, body):
   print (" Received %s" % (body))
   sleep(2) #I need stop it for two minutes

channel.basic_consume(callback, queue='queue_name', no_ack=True)
channel.start_consuming()

#function 2

def validator2(channel):    
channel.queue_declare(queue='queue_name')
print (' [*] Waiting messsaes for valiadtor2 press CTRL+C')


def callback(ch, method, properties, body):
  print (" Received %s" % (body))
  sleep(2) #I need stop it for two minutes

channel.basic_consume(callback, queue='queue_name', no_ack=True)
channel.start_consuming()



def manager():
 url = 'amqp://oamogcgg:xxxxxxxxxxxxxxxxxxxxxxxxx@salamander.rmq.cloudamqp.com/oamogcgg'
 params = pika.URLParameters(url)

#channel 1
 connection1= pika.BlockingConnection(params)
 channel1 = connection1.channel()

 channel1.exchange_declare(exchange='logs', exchange_type='fanout')
 result = channel1.queue_declare(queue='', exclusive=True)
 queue_name = result.method.queue
 channel1.queue_bind(exchange='logs', queue=queue_name)

#channel 2

 connection2= pika.BlockingConnection(params)
 channel2 = connection2.channel()


channel2.exchange_declare(exchange='logs', exchange_type='fanout')
result = channel2.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel2.queue_bind(exchange='logs', queue=queue_name)



#creating threads

t1 = threading.Thread(target=validator1, args=(channel1,))
t1.daemon = True
threads.append(t1)
t1.start()  

t2 = threading.Thread(target=valiadtor2, args=(channel2,))
t2.daemon = True
threads.append(t2)


t2.start()
for t in threads:
    t.join()


manager()

If your functions are dependent(by dependent I mean, one function works on the output of the other) then, you can call all those functions one by one in your callback function.如果您的函数是相关的(我的意思是,一个函数在另一个函数的输出上工作),那么您可以在回调函数中一一调用所有这些函数。 Once the last function is executed successfully you can acknowledge the message.成功执行最后一个功能后,您可以确认该消息。

If those functions are independent then you can maintain multiple queues for each of the function you wanna execute on the message.如果这些函数是独立的,那么您可以为要对消息执行的每个函数维护多个队列。 The same message can be routed to multiple queues using a fanout exchange as mentioned in RabbitMQ Tutorial-3 .可以使用RabbitMQ Tutorial-3 中提到的fanout交换将相同的消息路由到多个队列。

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

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