2017-08-23 24 views
1

我是新來的Python和鼠兔和我遇到了從使用BlockingConnection適配器,它保持了幾個小時後拋出異常隊列消耗的問題。的Python 3.6:如何創建使用SelectConnection適配器鼠消費者不使用類?

我爲此嘗試使用SelectConnection(異步)適配器,但我只能找到在類中使用此適配器類型的示例,並且使用基於類的代碼稍微超出了我目前的理解範圍。

我確實找到了一個例子,展示瞭如何使用SelectConnection創建一個生產者,但是我找不到一個消費者的例子,它讓我感到無休止的,因爲我原以爲pika網站會詳細描述一個基本的生產者和消費者,而不僅僅是一個生產...

生產者代碼如下,並從鼠網站採取(他們爲什麼不包括用於基本消費的例子是超越我...): (http://pika.readthedocs.io/en/latest/examples/comparing_publishing_sync_async.html

import pika 

# Step #3 
def on_open(connection): 

    connection.channel(on_channel_open) 

# Step #4 
def on_channel_open(channel): 

    channel.basic_publish('test_exchange', 
          'test_routing_key', 
          'message body value', 
          pika.BasicProperties(content_type='text/plain', 
               delivery_mode=1)) 

    connection.close() 

# Step #1: Connect to RabbitMQ 
parameters = pika.URLParameters('amqp://guest:[email protected]:5672/%2F') 

connection = pika.SelectConnection(parameters=parameters, 
            on_open_callback=on_open) 

try: 

Step #2 - Block on the IOLoop 
connection.ioloop.start() 

Catch a Keyboard Interrupt to make sure that the connection is closed cleanly 
except KeyboardInterrupt: 

# Gracefully close the connection 
connection.close() 

# Start the IOLoop again so Pika can communicate, it will stop on its own when the connection is closed 
connection.ioloop.start() 

任何人都可以告訴我如何修改此代碼爲'consu我」而不是‘產品’或者你可以指向我只使用基本的功能,而不是我發現的例子很多,但都沒有用我特定目的的基於類實例的任何實例...

謝謝您。 (你可能已經從我的問題的語氣中收集到了,我現在有點強調,因爲它的凌晨4點,我一直試圖解決這個問題好幾個小時!)

回答

相關問題