My first rabbitMQ program - hello_world

First make sure that rabbitMQ has been installed, that is, running rabbitMQ-server in the installation directory can be successful

Then do the following two steps:

First create a hello_world_send.py

  1.   
  2. import  them  
  3. import pika  
  4.   
  5. conn = None  
  6.   
  7. try:  
  8.     # get connection  
  9.     conn = pika.BlockingConnection(pika.ConnectionParameters('localhost'))  
  10.     # get channel  
  11.     channel = conn.channel()  
  12.   
  13.     # Before sending the queue, you need to determine whether the queue exists. If it does not exist, RabbitMQ will discard it. Create the queue first.  
  14.     channel.queue_declare('hello')  
  15.   
  16.     # Sending messages in RabbitMQ is not a direct queue, but an exchange. I won't do much research here, and the following series will gradually deepen  
  17.     ret = channel.basic_publish(exchange='',  
  18.                                 routing_key='hello',  
  19.                                 body="Hello, World!")  
  20.     print" [x] Sent 'Hello World!'"   
  21.     print  right  
  22. except Exception, e:  
  23.     raise e  
  24. finally:  
  25.     if conn:  
  26.         conn.close()

Then create a hello_world_recv.py

  1.  
  2. import  them  
  3. import pika  
  4.   
  5. conn = None  
  6.   
  7.   
  8. def callback(ch, method, properties, body):  
  9.     """ 
  10.         out body 
  11.     """  
  12.     print" [x] Recived ch {0}".format(ch)   
  13.   
  14.     print" [x] Recived method {0}".format(method)   
  15.   
  16.     print" [x] Recived properties {0}".format(properties)   
  17.   
  18.     print" [x] Recived %r" % (body, )   
  19.   
  20. try:  
  21.     # get connection  
  22.     conn = pika.BlockingConnection(pika.ConnectionParameters(  
  23.         'localhost')  
  24.     )  
  25.   
  26.     # get channel  
  27.     channel = conn.channel()  
  28.   
  29.     # declare queue, repeated declaration will not report an error, but if there is no queue, direct access will report an error  
  30.     channel.queue_declare('hello')  
  31.   
  32.     # get message  
  33.     channel.basic_consume(callback, queue='hello', no_ack=True)  
  34.   
  35.     print' [*] Waiting for messages. To exit press CTRL+C'   
  36.     channel.start_consuming()  
  37. except Exception, e:  
  38.     raise e  
  39. finally:  
  40.     if conn:  
  41.         conn.close() 

Now:

1: Run rabbitmq-server under sbin in the rabbitMQ installation directory

2: Run in the python file directory: python hello_world_recv.py

3: Run in the python file directory: python hello_world_send.py

After success you can see

  1. $ python send_helloworld.py   
  2.  [x] Sent 'Hello World!'  
  3. True 
  1. $ python recv_helloworld.py   
  2.  [*] Waiting for messages. To exit press CTRL+C  
  3.  [x] Recived ch <pika.adapters.blocking_connection.BlockingChannel object at 0x7f61ecc6fa90>  
  4.  [x] Recived method <Basic.Deliver(['consumer_tag=ctag1.6c2c709930904468b40d0e1a758f7aca', 'delivery_tag=1', 'exchange=', 'redelivered=False', 'routing_key=hello'])>  
  5.  [x] Recived properties <BasicProperties>  
  6.  [x] Recived 'Hello, World!' 

After the running program starts, it has been waiting to get the message, which can be aborted by Ctrl-C. Hello World is basically over, you basically know the basic usage of RabbitMQ, and the usage scenarios of RabbitMQ will be further analyzed in the future.

A reference URL is given below:

https://blog.csdn.net/fgf00/article/details/52872730

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325658257&siteId=291194637