Home > Article > Backend Development > Use of Python rabbitmq (4)
The third article explains about the use of switches. It can already send messages to all receivers. However, if you need to customize it freely, some messages are sent to some receivers and some messages are sent to other receivers. What should I do? What to do? In this case, routing keys are used.
#The working principle of the routing key: When the message queue of each receiving end is bound to the switch, the corresponding routing key can be set. When the sender sends information through the switch, it can specify the routing key, and the switch will send the message to the corresponding message queue based on the routing key, so that the receiving end can receive the message.
Following the previous article, we still use send.py and receive.py to simulate the function of routing keys. send.py represents the sending end, and receive.py represents the receiving end. The function of the instance is to send three levels of information: info, warning, and error to different receiving ends.
send.py code analysis
Compared with the previous article, the changes are mainly in two aspects:
Set the switch type (type) to direct. The previous article was set to fanout, which means broadcasting, and the message will be sent to all receivers. Here, setting it to direct means that the message will be sent according to the set routing key.
Set the routing key to be sent when sending information.
#!/usr/bin/env python #coding=utf8 import pika connection= pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel= connection.channel() #定义交换机,设置类型为direct channel.exchange_declare(exchange='messages',type='direct') #定义三个路由键 routings= ['info','warning','error'] #将消息依次发送到交换机,并设置路由键 for routingin routings: message= '%s message.' % routing channel.basic_publish(exchange='messages', routing_key=routing, body=message) print message connection.close()
receive.py code analysis
Compared with the third article, the changes are mainly in three aspects:
Settings The switch type (type) is direct.
Add the command line function to obtain parameters, and the parameters are routing keys.
When binding the queue to the switch, set the routing key.
#!/usr/bin/env python #coding=utf8 import pika, sys connection= pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel= connection.channel() #定义交换机,设置类型为direct channel.exchange_declare(exchange='messages',type='direct') #从命令行获取路由键参数,如果没有,则设置为info routings= sys.argv[1:] if not routings: routings= ['info'] #生成临时队列,并绑定到交换机上,设置路由键 result= channel.queue_declare(exclusive=True) queue_name= result.method.queue for routingin routings: channel.queue_bind(exchange='messages', queue=queue_name, routing_key=routing) def callback(ch, method, properties, body): print " [x] Received %r" % (body,) channel.basic_consume(callback, queue=queue_name, no_ack=True) print ' [*] Waiting for messages. To exit press CTRL+C' channel.start_consuming()
Open two terminals, one runs the code python receive.py info warning, which means that only info and warning messages are received. Run send.py on another terminal, and you can observe that the receiving terminal only receives info and warning messages. If you open multiple terminals and run receive.py and pass in different routing key parameters, you can see more obvious effects.
When the receiving end is running, you can use rabbitmqctl list_bindings to view the binding status.
The above is the content of the use of Python rabbitmq (4). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!