Messaging server Rabbmit
RabbitMQ Message Queuing
Python has two Q, threading queue, data interaction between different threads
Process queue: Interaction between different processes is a false statement. This is used to interact between the parent process and the child process, or to multiple sub-processes that belong to the same parent process.
There is no communication between the processes of the two Python programs.
Communication between individual processes:
QQ to send a message to the world. 1, through the socket, this need to write a lot of things themselves (dip bag, received what needs to return is what ...). 2. Message Queuing,
The two different programs to communicate, two machines to communicate?
Message Queuing: You can communicate with a variety of processes, and you don't have to write anything.
Zeromq\activemq These are message queues
Many events need to be experienced on their own.
Developed by Rabbitmq:erlang
Send message: Poll
Has the message been sent out and needs to be confirmed? Send it out or you're done.
Most of the time, you need to send back a message that has been processed. What should I do if I hang up in the process? The sender confiscated the response, right?
Hang up, the socket is broken, the sender is naturally aware that the message is immediately re-sent to another online receiving end.
no_ack=false# needs to get a response to give up the message.
The response is guaranteed: client, receive message end hangs Channel.basic_ack ()
Persistence: Guaranteed, after the server has been hung out
RABBITMQ Installation:
Need to install the environment: Erlang
Brew Install RABBITMQ
See Example:
Import Timeimport pikaimport uuidclass myrabbmit (object): Def __init__ (self): Self.connection = Pika. Blockingconnection (Pika. Connectionparameters ("localhost")) Self.channel = Self.connection.channel () # Self.chann El.queue_declare (queue= "rpc_queue") result = Self.channel.queue_declare (exclusive=true) self.callback_queue = Result.method.queue Self.channel.basic_consume (self.on_response, queue=self.cal Lback_queue, # No_ack=true) def on_response (self, CH, method, property, body): print (property.reply_to) print (body) Self.response = Body.decode () de F Call (self, cmd): self.response = None self.uuid = str (UUID.UUID4 ()) print (SELF.UUID) self.ch Annel.basic_publish (exchange= "", routing_key= "Rpc_queue", BODY =CMD, properties = Pika. Basicproperties (Reply_to=self.callback_queue, CORRELATION_ID=SELF.UUID)) while Self.respon SE is None:time.sleep (0.5) self.connection.process_data_events () # print ("Wait ...") return SELF.RESPONSEMY_RPC = Myrabbmit () cmd = input (">>"). Strip () If Cmd:result = My_rpc.call (cmd) print ("R Esult: ", result)
Import Osimport pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters ( "localhost")) Channel = Connection.channel () channel.queue_declare ("Rpc_queue") def func ( CMD): result = Os.popen (cmd). Read () print (Result) return resultdef on_response (ch, method, property, body ): print (body) print (property.reply_to, property.correlation_id) BODY = func (Body.decode ()) Channel.basic_publish (exchange= "", routing_key=property.reply_to, body=body ) Ch.basic_ack ( Delivery_tag=method.delivery_tag) # Reply to the results channel.basic_consume (on_response, queue= "Rpc_queue", no_ack =false, ) print ("Waiting to receive Message ...") channel.start_consuming ()
Python's fifth-stage learning record----rabbmit