日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

RabbitMQ 入门系列(9)— Python 的 pika 库常用函数及参数说明

發布時間:2023/11/27 生活经验 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RabbitMQ 入门系列(9)— Python 的 pika 库常用函数及参数说明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. pika.PlainCredentials(username, password, erase_on_connect)

  1. 功能:創建連接時的登錄憑證
  2. 參數:
  • username: MQ 賬號
  • password: MQ 密碼
  • erase_on_connect: 刪除連接上的憑據, 默認為 False
credentials = pika.PlainCredentials(username="guest", password="guest")

2. pika.ConnectionParameters(host, port, virtual_host, credentials)

  1. 功能: 連接 MQ 的參數設置
  2. 參數:
  • host: RabbitMQ IP 地址
  • port: RabbitMQ 端口
  • virtual_host: RabbitMQ 虛擬主機
  • credentials: 登錄憑證
pika.ConnectionParameters(host='127.0.0.1', port=5672, virtual_host='/', credentials=credentials)

3. pika.BlockingConnection(parameters)

  1. 功能: 阻塞式連接 MQ
  2. 參數:
  • parameters: 連接參數(包含主機/端口/虛擬主機/賬號/密碼等憑證信息)
connect = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1', port=5672, virtual_host='/', credentials=credentials))

4. pika.channel(channel_number)

  1. 功能: 創建信道
  2. 參數:
  • channel_number: 信道個數, 一般采用默認值 None
channel = connect.channel()

5. channel.exchange_declare(callback,exchange,exchange_type,passive,durable,auto_delete,internal,nowait,arguments)

  1. 功能: 聲明交換器
  2. 參數:
  • callback=None : 當 Exchange.DeclareOk 時 調用該方法, 當 nowait=True 該值必須為 None
  • exchange=None: 交換器名稱,保持非空,由字母、數字、連字符、下劃線、句號組成
  • exchange_type=‘direct’: 交換器類型
  • passive=False: 執行一個聲明或檢查它是否存在
  • durable=False: RabbitMQ 重啟時保持該交換器的持久性,即不會丟失
  • auto_delete=False: 沒有隊列綁定到該交換器時,自動刪除該交換器
  • internal=False: 只能由其它交換器發布-Can only be published to by other exchanges
  • nowait=False: 不需要 Exchange.DeclareOk 的響應-Do not expect an Exchange.DeclareOk response
  • arguments=None: 對該交換器自定義的鍵/值對, 默認為空
channel.exchange_declare(exchange='hello', exchange_type='direct', passive=False, durable=True, auto_delete=False)

6. channel.queue_declare(callback,queue,passive,durable,exclusive,auto_delete,nowait,arguments)

  1. 功能: 聲明隊列
  2. 參數:
  • callback : 當 Queue.DeclareOk 時的回調方法; 當 nowait=True 時必須為 None.
  • queue=’’ : 隊列名稱
  • passive=False : 只檢查隊列是否存在
  • durable=False : 當 RabbitMQ 重啟時,隊列保持持久性
  • exclusive=False : 僅僅允許當前的連接訪問
  • auto_delete=False : 當消費者取消或者斷開連接時, 自動刪除該隊列
  • nowait=False : 當 Queue.DeclareOk 時不需要等待
  • arguments=None : 對該隊列自定義鍵/值對
channel.queue_declare(queue='hello')

7. channel.queue_bind(callback, queue, exchange,routing_key,nowait,arguments)

  1. 功能: 通過路由鍵將隊列和交換器綁定
  2. 參數:
  • callback: 當 Queue.BindOk 時的回調函數, 當 nowait=True 時必須為 None
  • queue: 要綁定到交換器的隊列名稱
  • exchange: 要綁定的源交換器
  • routing_key=None: 綁定的路由鍵
  • nowait=False: 不需要 Queue.BindOk 的響應
  • arguments=None: 對該綁定自定義鍵/值對
channel.queue_bind(queue='hello', exchange='hello', routing_key='world')

8. channel.basic_publish(exchange, routing_key, body, properties, mandatory, immediate)

  1. 功能: 將消息發布到 RabbitMQ 交換器上
  2. 參數:
  • exchange: 要發布的目標交換器
  • routing_key: 該交換器所綁定的路由鍵
  • body: 攜帶的消息主體
  • properties=None: 消息的屬性,即文本/二進制等等
  • mandatory=False: 當 mandatory 參數設置為 true 時,交換機無法根據自身的路由鍵找到一個符合的隊列,
    那么 RabbitMQ 會調用 Basic.Return 命令將消息返回給生產者,
    當 mandatory 參數設置為 false 時,出現上述情況,消息會被丟棄
  • immediate=False: 立即性標志
channel.basic_publish(exchange='hello',  routing_key='world',  properties=msg_props, body=message)

9. channel.basic_consume(consumer_callback, queue, no_ack, exclusive, consumer_tag, arguments)

  1. 功能: 從隊列中拿到消息開始消費
  2. 參數:
  • consumer_callback:
    先來看官方的解釋
The function to call when consuming with the signature 
consumer_callback(channel, method, properties,body),wherechannel: pika.Channelmethod: pika.spec.Basic.Deliverproperties: pika.spec.BasicPropertiesbody: str, unicode, or bytes (python 3.x)

意思是說,當要消費時,調用該回調函數 consumer_callback, 函數的參數有channel, method, properties,body

  • queue=’’: 要消費的消息隊列
  • no_ack=False: 自動確認已經消費成功
  • exclusive=False: 不允許其它的消費者消費該隊列
  • consumer_tag=None: 指定自己的消費標記
  • arguments=None: 對該消費者自定義設置鍵值對
def callback(channel, method, properties, body):# 消息確認channel.basic_ack(delivery_tag=method.delivery_tag)if body.decode('utf-8') == "quit":# 停止消費,并退出channel.basic_cancel(consumer_tag='hello-consumer')channel.close()connect.close()else:print("msg is {}".format(body))channel.basic_consume(callback, queue='hello', no_ack=False)

10. channel.basic_ack()

  1. 功能: 消息確認
  2. 參數:
  • delivery_tag=0 : 服務端分配的傳遞標識
  • multiple=False:

If set to True, the delivery tag is treated as “up to and including”, so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

channel.basic_ack(delivery_tag=method.delivery_tag)

11. channel.basic_cancel(callback, consumer_tag, nowait)

  1. 功能: 取消消費, 該方法不會影響已經發送的消息,但是不會再發送新的消息給消費者
  2. 參數:
  • callback=None : 當 Basic.CancelOk 響應時的回調函數; 當 nowait=True 時必須為 None. 當 nowait=False 時必須是可回調的函數
  • consumer_tag=’’: 消費標識
  • nowait=False : 不期望得到 Basic.CancelOk response

If set to True, the delivery tag is treated as “up to and including”, so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

channel.basic_cancel(consumer_tag='hello-consumer')

12. channel.start_consuming()

  1. 功能: 處理 I/O 事件和 basic_consume 的回調, 直到所有的消費者被取消
  2. 參數:
    NA
channel.start_consuming()

13. channel.basic_reject(delivery_tag, requeue=True)

  1. 功能: 拒絕單條消息
  2. 參數:
  • delivery_tag : 傳遞標簽
  • requeue=True : 是否重新放回到隊列中去
channel.basic_reject()

14. channel.basic_nack(delivery_tag=None, multiple=False, requeue=True)

  1. 功能: 拒絕單條或者多條消息
  2. 參數:
  • delivery_tag=None : 傳遞標簽
  • multiple=False : 是否批量,即多條消息

If set to True, the delivery tag is treated as “up to and including”, so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

  • requeue=True: 是否重新放回到隊列中去
channel.basic_nack()

14. channel.queue_delete(callback=None,queue=’’,if_unused=False,if_empty=False,nowait=False)

  1. 功能: 刪除已聲明的隊列
  2. 參數:
  • callback=None: The callback to call on Queue.DeleteOk; MUST be None when nowait=True
  • queue=’’: The queue to delete
  • if_unused=False: only delete if it’s unused
  • if_empty=False: only delete if the queue is empty
  • nowait=False: Do not wait for a Queue.DeleteOk
channel.queue_delete()

15. channel.exchange_delete(callback=None,exchange=None,if_unused=False,nowait=False)

  1. 功能: 刪除已聲明的交換器
  2. 參數:
  • callback=None: The function to call on Exchange.DeleteOk; MUST be None when nowait=True.
  • exchange=None: The exchange name
  • if_unused=False: only delete if the exchange is unused
  • nowait=False: Do not wait for an Exchange.DeleteOk
channel.exchange_delete()

16. pika.BasicProperties()

  1. 功能: 發送消息的屬性
  2. 參數:
  • content_type=None,
  • content_encoding=None,
  • headers=None,
  • delivery_mode=None, 聲明信息持久化, 使信息持久化,需要聲明queue持久化和delivery_mode=2信息持久化
  • priority=None,
  • correlation_id=None,
  • reply_to=None,
  • expiration=None,
  • message_id=None,
  • timestamp=None,
  • type=None,
  • user_id=None,
  • app_id=None,
  • cluster_id=None
msg_props = pika.BasicProperties()
msg_props.content_type = 'text/plain'

17. 回調函數 callback

  1. 功能: 回調函數
  2. 參數:
  • channel: 包含channel的一切屬性和方法
  • method: 包含 consumer_tag, delivery_tag, exchange, redelivered, routing_key
  • properties: basic_publish 通過 properties 傳入的參數
  • body: basic_publish發送的消息
def callback(channel, method, properties, body):# 消息確認channel.basic_ack(delivery_tag=method.delivery_tag)if body.decode('utf-8') == "quit":# 停止消費,并退出channel.basic_cancel(consumer_tag='hello-consumer')channel.close()connect.close()else:print("msg is {}".format(body))

總結

以上是生活随笔為你收集整理的RabbitMQ 入门系列(9)— Python 的 pika 库常用函数及参数说明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。