@hainingwyx
2020-10-09T21:20:33.000000Z
字数 12470
阅读 813
rabbitmq
示例代码:Direct交换器
import rabbitpy
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
exchange = rabbitpy.Exchange(channel, 'direct-example',
exchange_type='direct')
exchange.declare()
RPC worker消费图片实现面部识别,将结果发回给消息发布方。
注意:RabbitMQ最大帧大小为131072字节,,消息体超过这个大小,就需要在AMQP协议级别分块。预先分配占用7字节,因此,每个消息体帧只能承载131065字节图片数据。
示例代码:RPC Publisher
import os
import rabbitpy
import time
from ch6 import utils
# Open the channel and connection
connection = rabbitpy.Connection()
channel = connection.channel()
exchange = rabbitpy.DirectExchange(channel, 'rpc-replies')
exchange.declare()
# Create the response queue that will automatically delete, is not durable and
# is exclusive to this publisher
queue_name = 'response-queue-%s' % os.getpid()
response_queue = rabbitpy.Queue(channel,
queue_name,
auto_delete=True,
durable=False,
exclusive=True)
# Declare the response queue
if response_queue.declare():
print('Response queue declared')
# Bind the response queue
if response_queue.bind('rpc-replies', queue_name):
print('Response queue bound')
# Iterate through the images to send RPC requests for
for img_id, filename in enumerate(utils.get_images()):
print('Sending request for image #%s: %s' % (img_id, filename))
# Create the message
message = rabbitpy.Message(channel,
utils.read_image(filename),
{'content_type': utils.mime_type(filename),
'correlation_id': str(img_id),
'reply_to': queue_name},
opinionated=True)
# Pubish the message
message.publish('direct-rpc-requests', 'detect-faces')
# Loop until there is a response message
message = None
while not message:
time.sleep(0.5)
message = response_queue.get()
# Ack the response message
message.ack()
# Caculate how long it took from publish to response
duration = (time.time() -
time.mktime(message.properties['headers']['first_publish']))
print('Facial detection RPC call for image %s total duration: %s' %
(message.properties['correlation_id'], duration))
# Display the image in the IPython notebook interface
utils.display_image(message.body, message.properties['content_type'])
print('RPC requests processed')
# Close the channel and connection
channel.close()
connection.close()
示例代码:RPC worker
import os
import rabbitpy
import time
from ch6 import detect
from ch6 import utils
# Open the connection and the channel
connection = rabbitpy.Connection()
channel = connection.channel()
# Create the worker queue
queue_name = 'rpc-worker-%s' % os.getpid()
queue = rabbitpy.Queue(channel, queue_name,
auto_delete=True,
durable=False,
exclusive=True)
# Declare the worker queue
if queue.declare():
print('Worker queue declared')
# Bind the worker queue
if queue.bind('direct-rpc-requests', 'detect-faces'):
print('Worker queue bound')
# Consume messages from RabbitMQ
for message in queue.consume_messages():
# Display how long it took for the message to get here
duration = time.time() - int(message.properties['timestamp'].strftime('%s'))
print('Received RPC request published %.2f seconds ago' % duration)
# Write out the message body to a temp file for facial detection process
temp_file = utils.write_temp_file(message.body,
message.properties['content_type'])
# Detect faces
result_file = detect.faces(temp_file)
# Build response properties including the timestamp from the first publish
properties = {'app_id': 'Chapter 6 Listing 2 Consumer',
'content_type': message.properties['content_type'],
'correlation_id': message.properties['correlation_id'],
'headers': {
'first_publish': message.properties['timestamp']}}
# The result file could just be the original image if nothing detected
body = utils.read_image(result_file)
# Remove the temp file
os.unlink(temp_file)
# Remove the result file
os.unlink(result_file)
# Publish the response response
response = rabbitpy.Message(channel, body, properties, opinionated=True)
response.publish('rpc-replies', message.properties['reply_to'])
# Acknowledge the delivery of the RPC request message
message.ack()
示例代码
import rabbitpy
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
exchange = rabbitpy.Exchange(channel,
'fanout-rpc-requests',
exchange_type='fanout')
exchange.declare()
示例程序:Publisher
import os
import rabbitpy
import time
from ch6 import utils
# Open the channel and connection
connection = rabbitpy.Connection()
channel = connection.channel()
# Create the response queue that will automatically delete, is not durable and
# is exclusive to this publisher
queue_name = 'response-queue-%s' % os.getpid()
response_queue = rabbitpy.Queue(channel,
queue_name,
auto_delete=True,
durable=False,
exclusive=True)
# Declare the response queue
if response_queue.declare():
print('Response queue declared')
# Bind the response queue
if response_queue.bind('rpc-replies', queue_name):
print('Response queue bound')
# Iterate through the images to send RPC requests for
for img_id, filename in enumerate(utils.get_images()):
print 'Sending request for image #%s: %s' % (img_id, filename)
# Create the message
message = rabbitpy.Message(channel,
utils.read_image(filename),
{'content_type': utils.mime_type(filename),
'correlation_id': str(img_id),
'reply_to': queue_name},
opinionated=True)
# Pubish the message
message.publish('fanout-rpc-requests')
# Loop until there is a response message
message = None
while not message:
time.sleep(0.5)
message = response_queue.get()
# Ack the response message
message.ack()
# Caculate how long it took from publish to response
duration = (time.time() -
time.mktime(message.properties['headers']['first_publish']))
print('Facial detection RPC call for image %s total duration: %s' %
(message.properties['correlation_id'], duration))
# Display the image in the IPython notebook interface
utils.display_image(message.body, message.properties['content_type'])
print 'RPC requests processed'
# Close the channel and connection
channel.close()
connection.close()
示例程序:detect worker
import os
import rabbitpy
import time
from ch6 import detect
from ch6 import utils
# Open the connection and the channel
connection = rabbitpy.Connection()
channel = connection.channel()
# Create the worker queue
queue_name = 'rpc-worker-%s' % os.getpid()
queue = rabbitpy.Queue(channel, queue_name,
auto_delete=True,
durable=False,
exclusive=True)
# Declare the worker queue
if queue.declare():
print('Worker queue declared')
# Bind the worker queue
if queue.bind('fanout-rpc-requests'):
print('Worker queue bound')
# Consume messages from RabbitMQ
for message in queue.consume_messages():
# Display how long it took for the message to get here
duration = time.time() - int(message.properties['timestamp'].strftime('%s'))
print('Received RPC request published %.2f seconds ago' % duration)
# Write out the message body to a temp file for facial detection process
temp_file = utils.write_temp_file(message.body,
message.properties['content_type'])
# Detect faces
result_file = detect.faces(temp_file)
# Build response properties including the timestamp from the first publish
properties = {'app_id': 'Chapter 6 Listing 2 Consumer',
'content_type': message.properties['content_type'],
'correlation_id': message.properties['correlation_id'],
'headers': {
'first_publish': message.properties['timestamp']}}
# The result file could just be the original image if nothing detected
body = utils.read_image(result_file)
# Remove the temp file
os.unlink(temp_file)
# Remove the result file
os.unlink(result_file)
# Publish the response response
response = rabbitpy.Message(channel, body, properties)
response.publish('rpc-replies', message.properties['reply_to'])
# Acknowledge the delivery of the RPC request message
message.ack()
示例程序:Hash Consumer
import os
import hashlib
import rabbitpy
# Open the connection and the channel
connection = rabbitpy.Connection()
channel = connection.channel()
# Create the worker queue
queue_name = 'hashing-worker-%s' % os.getpid()
queue = rabbitpy.Queue(channel, queue_name,
auto_delete=True,
durable=False,
exclusive=True)
# Declare the worker queue
if queue.declare():
print('Worker queue declared')
# Bind the worker queue
if queue.bind('fanout-rpc-requests'):
print('Worker queue bound')
# Consume messages from RabbitMQ
for message in queue.consume_messages():
# Create the hashing object
hash_obj = hashlib.md5(message.body)
# Print out the info, this might go into a database or log file
print('Image with correlation-id of %s has a hash of %s' %
(message.properties['correlation_id'],
hash_obj.hexdigest()))
# Acknowledge the delivery of the RPC request message
message.ack()
示例代码
import rabbitpy
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
exchange = rabbitpy.Exchange(channel,
'headers-rpc-requests',
exchange_type='headers')
exchange.declare()
示例程序:publisher
import os
import rabbitpy
import time
from ch6 import utils
# Open the channel and connection
connection = rabbitpy.Connection()
channel = connection.channel()
# Create the response queue that will automatically delete, is not durable and
# is exclusive to this publisher
queue_name = 'response-queue-%s' % os.getpid()
response_queue = rabbitpy.Queue(channel,
queue_name,
auto_delete=True,
durable=False,
exclusive=True)
# Declare the response queue
if response_queue.declare():
print('Response queue declared')
# Bind the response queue
if response_queue.bind('rpc-replies', queue_name):
print('Response queue bound')
# Iterate through the images to send RPC requests for
for img_id, filename in enumerate(utils.get_images()):
print('Sending request for image #%s: %s' % (img_id, filename))
# Create the message
message = rabbitpy.Message(channel,
utils.read_image(filename),
{'content_type': utils.mime_type(filename),
'correlation_id': str(img_id),
'headers': {'source': 'profile',
'object': 'image',
'action': 'new'},
'reply_to': queue_name},
opinionated=True)
# Pubish the message
message.publish('headers-rpc-requests')
# Loop until there is a response message
message = None
while not message:
time.sleep(0.5)
message = response_queue.get()
# Ack the response message
message.ack()
# Caculate how long it took from publish to response
duration = (time.time() -
time.mktime(message.properties['headers']['first_publish']))
print('Facial detection RPC call for image %s total duration: %s' %
(message.properties['correlation_id'], duration))
# Display the image in the IPython notebook interface
utils.display_image(message.body, message.properties['content_type'])
print('RPC requests processed')
# Close the channel and connection
channel.close()
connection.close()
示例程序:worker
import os
import rabbitpy
import time
from ch6 import detect
from ch6 import utils
# Open the connection and the channel
connection = rabbitpy.Connection()
channel = connection.channel()
# Create the worker queue
queue_name = 'rpc-worker-%s' % os.getpid()
queue = rabbitpy.Queue(channel, queue_name,
auto_delete=True,
durable=False,
exclusive=True)
# Declare the worker queue
if queue.declare():
print('Worker queue declared')
# Bind the worker queue
if queue.bind('headers-rpc-requests',
arguments={'x-match': 'all',
'source': 'profile',
'object': 'image',
'action': 'new'}):
print('Worker queue bound')
# Consume messages from RabbitMQ
for message in queue.consume_messages():
# Display how long it took for the message to get here
duration = time.time() - int(message.properties['timestamp'].strftime('%s'))
print('Received RPC request published %.2f seconds ago' % duration)
# Write out the message body to a temp file for facial detection process
temp_file = utils.write_temp_file(message.body,
message.properties['content_type'])
# Detect faces
result_file = detect.faces(temp_file)
# Build response properties including the timestamp from the first publish
properties = {'app_id': 'Chapter 6 Listing 2 Consumer',
'content_type': message.properties['content_type'],
'correlation_id': message.properties['correlation_id'],
'headers': {
'first_publish': message.properties['timestamp']}}
# The result file could just be the original image if nothing detected
body = utils.read_image(result_file)
# Remove the temp file
os.unlink(temp_file)
# Remove the result file
os.unlink(result_file)
# Publish the response response
response = rabbitpy.Message(channel, body, properties, opinionated=True)
response.publish('rpc-replies', message.properties['reply_to'])
# Acknowledge the delivery of the RPC request message
message.ack()
交换器间绑定,使用RPC方法Exchange.Bind。
示例代码:
import rabbitpy
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
tpc = rabbitpy.Exchange(channel, 'events',
exchange_type='topic')
tpc.declare()
xch = rabbitpy.Exchange(channel, 'distributed-events',
exchange_type='x-consistent-hash')
xch.declare()
xch.bind(foo, '#')
用于消息队列的负载均衡,可以提升吞吐量
示例代码:采用路由键的哈希值来分发消息
import rabbitpy
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
exchange = rabbitpy.Exchange(channel, 'image-storage',
exchange_type='x-consistent-hash')
exchange.declare()
示例代码:header中的属性值作为哈希值
import rabbitpy
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
exchange = rabbitpy.Exchange(channel, 'image-storage',
exchange_type='x-consistent-hash',
arguments={'hash-header': 'image-hash'})
exchange.declare()
示例代码:队列的创建与绑定
import rabbitpy
with rabbitpy.Connection() as connection:
with connection.channel() as channel:
for queue_num in range(4):
queue = rabbitpy.Queue (channel, 'server%s' % queue_num)
queue.declare()
queue.bind('image-storage', '10')