RabbitMQ生產者消費者模型

2021-09-25 16:40:27 字數 1043 閱讀 9763

生產者

mport  pika

connection = pika.blockingconnection(pika.connectionparameters(host='127.0.0.1')) # 建立乙個例項

channel = connection.channel() # 宣告乙個管道

channel.queue_declare(queue='hello ') # 宣告乙個queue

channel.basic_publish(exchange='',

routing_key='hello', # queue名字

body='hello word' # 訊息內容

)print("[x] sent 'hello word!'")

connection.close()

消費者

import pika

connection = pika.blockingconnection(pika.connectionparameters(host='127.0.0.1')) # 建立乙個例項

channel = connection.channel() # 宣告乙個管道

channel.queue_declare(queue='hello') # 宣告乙個queue

def callback(ch, methon, properties, body): # ch 表示channel的記憶體位址

print('0000', ch, methon)

print("[x] received %r", body)

channel.basic_consume(# 開始消費訊息

'hello', # 佇列名

callback , # 收到訊息就呼叫callback來接收訊息

auto_ack=true)

print('[*] waiting for message')

channel.start_consuming()

生產者消費者 生產者與消費者模式

一 什麼是生產者與消費者模式 其實生產者與消費者模式就是乙個多執行緒併發協作的模式,在這個模式中呢,一部分執行緒被用於去生產資料,另一部分執行緒去處理資料,於是便有了形象的生產者與消費者了。而為了更好的優化生產者與消費者的關係,便設立乙個緩衝區,也就相當於乙個資料倉儲,當生產者生產資料時鎖住倉庫,不...

redis stream 實現生產者消費者模式

test public void producer throws interruptedexception test public void consumer1 throws interruptedexception list msg jedis.xreadgroup groupname,consu...

生產者消費者

using system using system.collections.generic using system.threading namespace gmservice foreach thread thread in producers q.exit console.read public...