最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python隊列通信:rabbitMQ的使用(實(shí)例講解)

 更新時間:2017年12月22日 15:55:19   作者:ywq935  
下面小編就為大家分享一篇python隊列通信:rabbitMQ的使用(實(shí)例講解),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

(一)、前言

為什么引入消息隊列?

1.程序解耦

2.提升性能

3.降低多業(yè)務(wù)邏輯復(fù)雜度

(二)、python操作rabbit mq

rabbitmq配置安裝基本使用參見上節(jié)文章,不再復(fù)述。

若想使用python操作rabbitmq,需安裝pika模塊,直接pip安裝:

pip install pika

1.最簡單的rabbitmq producer端與consumer端對話:

producer:

#Author :ywq
import pika
auth=pika.PlainCredentials('ywq','qwe') #save auth indo
connection = pika.BlockingConnection(pika.ConnectionParameters(
  '192.168.0.158',5672,'/',auth)) #connect to rabbit
channel = connection.channel() #create channel
channel.queue_declare(queue='hello') #declare queue
#n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
   routing_key='hello',
   body='Hello World!') #the body is the msg content
print(" [x] Sent 'Hello World!'")
connection.close()

consumer:

#Author :ywq
import pika
auth=pika.PlainCredentials('ywq','qwe') #auth info
connection = pika.BlockingConnection(pika.ConnectionParameters(
  '192.168.0.158',5672,'/',auth)) #connect to rabbit
channel = connection.channel()  #create channel

channel.queue_declare(queue='hello') #decalre queue
def callback(ch, method, properties, body):
 print(" [x] Received %r" % body)

channel.basic_consume(callback,
   queue='hello',
   no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

消息傳遞消費(fèi)過程中,可以在rabbit web管理頁面實(shí)時查看隊列消息信息。

2.持久化的消息隊列,避免宕機(jī)等意外情況造成消息隊列丟失。

consumer端無需改變,在producer端代碼內(nèi)加上兩個屬性,分別使消息持久化、隊列持久化,只選其一還是會出現(xiàn)消息丟失,必須同時開啟:

delivery_mode=2 #make msg persisdent
durable=True

屬性插入位置見如下代碼(producer端):

#Author :ywq
import pika,sys
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
  '192.168.0.158',5672,'/',auth_info
 ))
channel=connection.channel()
channel.queue_declare(queue='test1',durable=True) #durable=Ture, make queue persistent

msg=''.join(sys.argv[1:]) or 'Hello'
channel.basic_publish(
 exchange='',
 routing_key='test1',
 body=msg,
 properties=pika.BasicProperties(
  delivery_mode=2 #make msg persisdent
 )
)

print('Send done:',msg)
connection.close()

3.公平分發(fā)

在多consumer的情況下,默認(rèn)rabbit是輪詢發(fā)送消息的,但有的consumer消費(fèi)速度快,有的消費(fèi)速度慢,為了資源使用更平衡,引入ack確認(rèn)機(jī)制。consumer消費(fèi)完消息后會給rabbit發(fā)送ack,一旦未ack的消息數(shù)量超過指定允許的數(shù)量,則不再往該consumer發(fā)送,改為發(fā)送給其他consumer。

producer端代碼不用改變,需要給consumer端代碼插入兩個屬性:

channel.basic_qos(prefetch_count= *) #define the max non_ack_count
channel.basic_ack(delivery_tag=deliver.delivery_tag) #send ack to rabbitmq

屬性插入位置見如下代碼(consumer端):

#Author :ywq
import pika,time
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
 '192.168.0.158',5672,'/',auth_info
 )
)
channel=connection.channel()
channel.queue_declare(queue='test2',durable=True)
def callback(chann,deliver,properties,body):
 print('Recv:',body)
 time.sleep(5)
 chann.basic_ack(delivery_tag=deliver.delivery_tag) #send ack to rabbit
channel.basic_qos(prefetch_count=1)
'''
注意,no_ack=False 注意,這里的no_ack類型僅僅是告訴rabbit該消費(fèi)者隊列是否返回ack,若要返回ack,需要在callback內(nèi)定義
prefetch_count=1,未ack的msg數(shù)量超過1個,則此consumer不再接受msg,此配置需寫在channel.basic_consume上方,否則會造成non_ack情況出現(xiàn)。
'''
channel.basic_consume(
 callback,
 queue='test2'
)

channel.start_consuming()

三、消息發(fā)布/訂閱

上方的幾種模式都是producer端發(fā)送一次,則consumer端接收一次,能不能實(shí)現(xiàn)一個producer發(fā)送,多個關(guān)聯(lián)的consumer同時接收呢?of course,rabbit支持消息發(fā)布訂閱,共支持三種模式,通過組件exchange轉(zhuǎn)發(fā)器,實(shí)現(xiàn)3種模式:

fanout: 所有bind到此exchange的queue都可以接收消息,類似廣播。

direct: 通過routingKey和exchange決定的哪個唯一的queue可以接收消息,推送給綁定了該queue的consumer,類似組播。

topic:所有符合routingKey(此時可以是一個表達(dá)式)的routingKey所bind的queue可以接收消息,類似前綴列表匹配路由。

1.fanout

publish端(producer):

#Author :ywq
import pika,sys,time
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
 '192.168.0.158',5672,'/',auth_info
 )
)
channel=connection.channel()
channel.exchange_declare(exchange='hello',
    exchange_type='fanout'
    )
msg=''.join(sys.argv[1:]) or 'Hello world %s' %time.time()
channel.basic_publish(
 exchange='hello',
 routing_key='',
 body=msg,
 properties=pika.BasicProperties(
 delivery_mode=2
 )
)
print('send done')
connection.close()

subscribe端(consumer):

#Author :ywq
import pika
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
 '192.168.0.158',5672,'/',auth_info
 )
)
channel=connection.channel()
channel.exchange_declare(
 exchange='hello',
 exchange_type='fanout'
)
random_num=channel.queue_declare(exclusive=True) #隨機(jī)與rabbit建立一個queue,comsumer斷開后,該queue立即刪除釋放
queue_name=random_num.method.queue
channel.basic_qos(prefetch_count=1)
channel.queue_bind(
 queue=queue_name,
 exchange='hello'
)
def callback(chann,deliver,properties,body):
 print('Recv:',body)
 chann.basic_ack(delivery_tag=deliver.delivery_tag) #send ack to rabbit

channel.basic_consume(
 callback,
 queue=queue_name,
)
channel.start_consuming()

實(shí)現(xiàn)producer一次發(fā)送,多個關(guān)聯(lián)consumer接收。

使用exchange模式時:

1.producer端不再申明queue,直接申明exchange

2.consumer端仍需綁定隊列并指定exchange來接收message

3.consumer最好創(chuàng)建隨機(jī)queue,使用完后立即釋放。

隨機(jī)隊列名在web下可以檢測到:

2.direct

使用exchange同時consumer有選擇性的接收消息。隊列綁定關(guān)鍵字,producer將數(shù)據(jù)根據(jù)關(guān)鍵字發(fā)送到消息exchange,exchange根據(jù) 關(guān)鍵字 判定應(yīng)該將數(shù)據(jù)發(fā)送至指定隊列,consumer相應(yīng)接收。即在fanout基礎(chǔ)上增加了routing key.

producer:

#Author :ywq
import pika,sys
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
 '192.168.0.158',5672,'/',auth_info
 )
)
channel=connection.channel()
channel.exchange_declare(exchange='direct_log',
   exchange_type='direct',
   )
while True:
 route_key=input('Input routing key:')
 msg=''.join(sys.argv[1:]) or 'Hello'
 channel.basic_publish(
 exchange='direct_log',
 routing_key=route_key,
 body=msg,
 properties=pika.BasicProperties(
  delivery_mode=2
 )
 )
connection.close()

consumer:

#Author :ywq
import pika,sys
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
 '192.168.0.158',5672,'/',auth_info
))
channel=connection.channel()
channel.exchange_declare(
 exchange='direct_log',
 exchange_type='direct'
)
queue_num=channel.queue_declare(exclusive=True)
queue_name=queue_num.method.queue
route_key=input('Input routing key:')

channel.queue_bind(
 queue=queue_name,
 exchange='direct_log',
 routing_key=route_key
)
def callback(chann,deliver,property,body):
 print('Recv:[level:%s],[msg:%s]' %(route_key,body))
 chann.basic_ack(delivery_tag=deliver.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(
 callback,
 queue=queue_name
)
channel.start_consuming()

同時開啟多個consumer,其中兩個接收notice,兩個接收warning,運(yùn)行效果如下:

3.topic

相較于direct,topic能實(shí)現(xiàn)模糊匹配式工作方式(在consumer端指定匹配方式),只要routing key包含指定的關(guān)鍵字,則將該msg發(fā)往綁定的queue上。

rabbitmq通配符規(guī)則:

符號“#”匹配一個或多個詞,符號“”匹配一個詞。因此“abc.#”能夠匹配到“abc.m.n”,但是“abc.*‘' 只會匹配到“abc.m”?!?'號為分割符。使用通配符匹配時必須使用‘.'號分割。

producer:

#Author :ywq
import pika,sys
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
 '192.168.0.158',5672,'/',auth_info
 )
)
channel=connection.channel()
channel.exchange_declare(exchange='topic_log',
   exchange_type='topic',
   )
while True:
 route_key=input('Input routing key:')
 msg=''.join(sys.argv[1:]) or 'Hello'
 channel.basic_publish(
 exchange='topic_log',
 routing_key=route_key,
 body=msg,
 properties=pika.BasicProperties(
  delivery_mode=2
 )
 )
connection.close()

consumer:

#Author :ywq
import pika,sys
auth_info=pika.PlainCredentials('ywq','qwe')
connection=pika.BlockingConnection(pika.ConnectionParameters(
 '192.168.0.158',5672,'/',auth_info
))
channel=connection.channel()
channel.exchange_declare(
 exchange='topic_log',
 exchange_type='topic'
)
queue_num=channel.queue_declare(exclusive=True)
queue_name=queue_num.method.queue
route_key=input('Input routing key:')

channel.queue_bind(
 queue=queue_name,
 exchange='topic_log',
 routing_key=route_key
)
def callback(chann,deliver,property,body):
 print('Recv:[type:%s],[msg:%s]' %(route_key,body))
 chann.basic_ack(delivery_tag=deliver.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(
 callback,
 queue=queue_name
)
channel.start_consuming()

運(yùn)行效果:

rabbitmq三種publish/subscribe模型簡單介紹完畢。

以上這篇python隊列通信:rabbitMQ的使用(實(shí)例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在Python中使用gRPC的方法示例

    在Python中使用gRPC的方法示例

    這篇文章主要介紹了在Python中使用gRPC的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Python實(shí)現(xiàn)隊列的方法

    Python實(shí)現(xiàn)隊列的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)隊列的方法,實(shí)例分析了Python實(shí)現(xiàn)隊列的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • pytorch-RNN進(jìn)行回歸曲線預(yù)測方式

    pytorch-RNN進(jìn)行回歸曲線預(yù)測方式

    今天小編就為大家分享一篇pytorch-RNN進(jìn)行回歸曲線預(yù)測方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python traceback模塊獲取異常信息的使用

    Python traceback模塊獲取異常信息的使用

    Python的traceback模塊提供了多種方法來獲取和展示異常的堆棧信息,本文主要介紹了Python traceback模塊獲取異常信息的使用,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 膠水語言Python與C/C++的相互調(diào)用的實(shí)現(xiàn)

    膠水語言Python與C/C++的相互調(diào)用的實(shí)現(xiàn)

    這篇文章主要介紹了膠水語言Python與C/C++的相互調(diào)用的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 跟老齊學(xué)Python之集合(set)

    跟老齊學(xué)Python之集合(set)

    本文主要內(nèi)容是要向各位介紹一種新的數(shù)據(jù)類型:集合(set).徹底暈倒了,到底python有多少個數(shù)據(jù)類型呢?又多出來了一個.
    2014-09-09
  • Python自定義元類的實(shí)例講解

    Python自定義元類的實(shí)例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于Python自定義元類的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-03-03
  • pandas中apply和transform方法的性能比較及區(qū)別介紹

    pandas中apply和transform方法的性能比較及區(qū)別介紹

    這篇文章主要介紹了pandas中apply和transform方法的性能比較,在文中給大家講解了apply() 與transform()的相同點(diǎn)與不同點(diǎn),需要的朋友可以參考下
    2018-10-10
  • Python+Selenium使用Page Object實(shí)現(xiàn)頁面自動化測試

    Python+Selenium使用Page Object實(shí)現(xiàn)頁面自動化測試

    這篇文章主要介紹了Python+Selenium使用Page Object實(shí)現(xiàn)頁面自動化測試,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 關(guān)于Python形參打包與解包小技巧分享

    關(guān)于Python形參打包與解包小技巧分享

    今天小編就為大家分享一篇關(guān)于Python形參打包與解包小技巧分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評論

田东县| 宜都市| 新邵县| 紫金县| 江永县| 东台市| 新巴尔虎右旗| 监利县| 突泉县| 洛浦县| 恩施市| 抚顺县| 武陟县| 如东县| 新丰县| 临夏县| 霍山县| 桐城市| 麟游县| 延庆县| 呼伦贝尔市| 青海省| 安图县| 白城市| 珲春市| 明光市| 扎赉特旗| 竹山县| 衢州市| 乐清市| 鄂托克前旗| 龙川县| 黎川县| 根河市| 西安市| 塔城市| 松潘县| 太仓市| 凤凰县| 定结县| 溧水县|