rabbitMQ實戰(一)---------使用pika庫實現hello world

來源:互聯網
上載者:User

標籤:ascii   nbsp   hang   rabbit   param   decode   parameter   message   scribe   

rabbitMQ實戰(一)---------使用pika庫實現hello world2016-05-18 23:29 本站整理 瀏覽(267)  pika是RabbitMQ團隊編寫的官方Python AMQP庫。需要先安裝pika:pip3 install pika有較詳細的注釋,就不再詳細說明了生產者代碼:hello_world_producer.py:
import pika,sys#connect to the rabbitmq,use the default vhostcredentials = pika.PlainCredentials("guest","guest")conn_params = pika.ConnectionParameters("localhost",                                        credentials=credentials)conn_broker = pika.BlockingConnection(conn_params)#get a channel used to communicate with the rabbitmqchannel = conn_broker.channel()#declare a exchangechannel.exchange_declare(exchange=‘hello-exchange‘,                         type=‘direct‘,                         passive=False, #if the exchange already existes,report a error.It means we want to declare an exchange.                         durable=True, #durable the message                         auto_delete=False) #if the last consumer is over,do not delete the exchange auto#create a messagemsg = sys.argv[1]msg_props = pika.BasicProperties()msg_props.content_type = "text/plain"#publish the messagechannel.basic_publish(body=msg,                      exchange=‘hello-exchange‘,                      properties=msg_props,                      routing_key=‘hola‘)

 
消費者代碼
hello_world_consumer.py:
import pika#connect to the rabbitmq,use the default vhostcredentials = pika.PlainCredentials("guest","guest")conn_params = pika.ConnectionParameters("localhost",                                        credentials=credentials)conn_broker = pika.BlockingConnection(conn_params)#get a channel used to communicate with the rabbitmqchannel = conn_broker.channel()#declare a exchangechannel.exchange_declare(exchange=‘hello-exchange‘,                         type=‘direct‘,                         passive=False, #if the exchange already existes,report a error.It means we want to declare an exchange.                         durable=True, #durable the message                         auto_delete=False) #if the last consumer is over,do not delete the exchange auto#declare a queuechannel.queue_declare(queue="hello-queue")#bind queue to an exchangechannel.queue_bind(queue=‘hello-queue‘,                   exchange=‘hello-exchange‘,                   routing_key=‘hola‘)#define the consumer method to consumer message from a queuedef msg_consumer(channel,method,header,body):    channel.basic_ack(delivery_tag=method.delivery_tag)    if body.decode("ascii") == "quit":        channel.basic_cancel(consumer_tag=‘hello-consumer‘)        channel.stop_consuming()    else:        print(body)    return#subscribe messagechannel.basic_consume(msg_consumer,                      queue=‘hello-queue‘,                      consumer_tag=‘hello-consumer‘)#begin loop until a quit message is sentchannel.start_consuming()

 
運行代碼:
需要先運行consumer,因為我們是在消費者中建立隊列的,如果先生產訊息,由於沒有可以路由到的隊列,訊息會被丟棄。
$ python hello_world_consumer.pyb‘good‘b‘hello world‘
$ python hello_world_producer.py "good"$ python hello_world_producer.py "hello world"$ python hello_world_producer.py "quit"

 

rabbitMQ實戰(一)---------使用pika庫實現hello world

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.