product.py 

#!/usr/bin/python

#coding:utf-8

import pika

import random

import time

credentials = pika.PlainCredentials('rabbitadmin','xxxxx')

parameters = pika.ConnectionParameters('x.x.x.x',5672,'/',credentials)

connection = pika.BlockingConnection(parameters)

channel = connection.channel()

channel.queue_declare(queue='first_queue')

i = 0

while i < 5000:

    time.sleep(random.random())

    a = '{} message'.format(i)

    channel.basic_publish(exchange='',routing_key='first_queue',body=a)

    i += 1

connection.close()

receive.py 

#!/usr/bin/python

#coding:utf-8

import pika

def callback(ch, method, properties, body):

    print body

credentials = pika.PlainCredentials('rabbitadmin', 'xxxxx')  

parameters = pika.ConnectionParameters('x.x.x.x',5672,'/',credentials)    

connection = pika.BlockingConnection(parameters)    

channel = connection.channel()   

channel.queue_declare(queue='first_queue1')

channel.basic_consume(callback,queue='first_queue1',no_ack=True)

channel.start_consuming()

注意事项:receive.py 中最好声明下queue_declare 队列名,因为如果没有queue就会报错,导致程序出错!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注