pika的安装和简单用法

在pycharm上直接导入
先安装otp_win64_21.1.exe
再安装rabbitmq-server-3.7.8.exe
直接上代码

#coding:utf-8
import pika
import sys
#生产者
while True:
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost')
    channel = connection.channel()
    channel.queue_declare(queue='hello_1234',durable=True)
    body = raw_input(':')
    channel.basic_publish(
    exchange='', routing_key='hello_1234', body=body)
    print " [x] Sent '%s'"%body
    if body == 'quit':
   	  try:
         for i in range(10):
             channel.basic_publish(exchange='', routing_key='hello_1234',   body=body)
          except:
               pass
          finally:
              sys.exit()
connection.close()



import pika
import sys
#消费者
def get_message():
    connection = pika.BlockingConnection(pika.ConnectionParameters(
        'localhost',socket_timeout=1))
    channel = connection.channel()
    channel.queue_declare(queue='hello_1234',durable=True)
    channel.basic_consume(callback, queue='hello_1234', no_ack=False)
    print ' [*] Waiting for messages. To exit press CTRL+C'
    channel.start_consuming()
def callback(ch, method, properties, body):
      print "Received %r" % (body,)
      if body == 'quit':
           sys.exit()
get_message()

你可能感兴趣的:(程序间通信)