sudo apt-get install rabbitmq-server
sudo pip install pika
#!/usr/bin/env python
#coding=utf8
import pika
#连接rabbitmq服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
#定义队列
channel.queue_declare(queue='compute_queue')
print ' [*] Waiting for n'
#将n值加1
def increase(n):
return n + 1
#定义接收到消息的处理方法
def request(ch, method, properties, body):
print " [.] increase(%s)" % (body,)
response = increase(int(body))
#将计算结果发送回控制中心
ch.basic_publish(exchange='',
routing_key=properties.reply_to,
body=str(response))
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(request, queue='compute_queue')
channel.start_consuming()
#!/usr/bin/env python
#coding=utf8
import pika
class Center(object):
def __init__(self):
self.connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
self.channel = self.connection.channel()
#定义接收返回消息的队列
result = self.channel.queue_declare(exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(self.on_response,
no_ack=True,
queue=self.callback_queue)
#定义接收到返回消息的处理方法
def on_response(self, ch, method, props, body):
self.response = body
def request(self, n):
self.response = None
#发送计算请求,并声明返回队列
self.channel.basic_publish(exchange='',
routing_key='compute_queue',
properties=pika.BasicProperties(
reply_to = self.callback_queue,
),
body=str(n))
#接收返回的数据
while self.response is None:
self.connection.process_data_events()
return int(self.response)
center = Center()
print " [x] Requesting increase(30)"
response = center.request(30)
print " [.] Got %r" % (response,)
#!/usr/bin/env python
#coding=utf8
import pika
#连接rabbitmq服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
#定义队列
channel.queue_declare(queue='compute_queue')
print ' [*] Waiting for n'
#将n值加1
def increase(n):
return n + 1
#定义接收到消息的处理方法
def request(ch, method, props, body):
print " [.] increase(%s)" % (body,)
response = increase(int(body))
#将计算结果发送回控制中心,增加correlation_id的设定
ch.basic_publish(exchange='',
routing_key=props.reply_to,
properties=pika.BasicProperties(correlation_id = \
props.correlation_id),
body=str(response))
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(request, queue='compute_queue')
channel.start_consuming()
#!/usr/bin/env python
#coding=utf8
import pika, threading, uuid
#自定义线程类,继承threading.Thread
class MyThread(threading.Thread):
def __init__(self, func, num):
super(MyThread, self).__init__()
self.func = func
self.num = num
def run(self):
print " [x] Requesting increase(%d)" % self.num
response = self.func(self.num)
print " [.] increase(%d)=%d" % (self.num, response)
#控制中心类
class Center(object):
def __init__(self):
self.connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
self.channel = self.connection.channel()
#定义接收返回消息的队列
result = self.channel.queue_declare(exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(self.on_response,
no_ack=True,
queue=self.callback_queue)
#返回的结果都会存储在该字典里
self.response = {}
#定义接收到返回消息的处理方法
def on_response(self, ch, method, props, body):
self.response[props.correlation_id] = body
def request(self, n):
corr_id = str(uuid.uuid4())
self.response[corr_id] = None
#发送计算请求,并设定返回队列和correlation_id
self.channel.basic_publish(exchange='',
routing_key='compute_queue',
properties=pika.BasicProperties(
reply_to = self.callback_queue,
correlation_id = corr_id,
),
body=str(n))
#接收返回的数据
while self.response[corr_id] is None:
self.connection.process_data_events()
return int(self.response[corr_id])
center = Center()
#发起5次计算请求
nums= [10, 20, 30, 40 ,50]
threads = []
for num in nums:
threads.append(MyThread(center.request, num))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有