我有很長的編程背景,但是對Python來說是新手,並且正在與Tornado一起玩,爲某個長輪詢服務創建原型。如何在數據可用時完成Tornado長輪詢請求
我想要實現的是用戶連接到說example.com/get/1234,這是長期民意調查的一部分。 1234是用戶標識。目前,它只是掛起並等待內容。然後,用戶使用新標籤頁/其他瀏覽器/其他計算機/等,並轉到url,如example.com/set/1234?data=abcd,其中1234是用戶標識,數據是內容爲「abcd」的變量。現在,當發生這種情況時,第一個獲取請求應該打印出數據「abcd」並完成請求。用戶標識顯然用於允許多個用戶同時使用該服務。所以,簡單地說:
1)進入example.com/get/1234 - >等待 2)在另一個選項卡,打開example.com/set/1234?data=abcd 3)此請求後,右,第一請求打印出abcd並完成
下面是我一直在嘗試的東西,但我並沒有真正推進,也找不到適當的Google關鍵字來解決這個問題。
class GetHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
# Getting user ID is working
def get(self, user_id):
# This is something I'm not sure is right, but found it in an
# example. I haven't created the code_received function yet,
# nor know if it should be here? Should the code_received
# and connection finishing be called from the /set part?
cursor = self.get_argument("cursor", None)
self.wait_for_code(self.code_received, cursor=cursor)
def code_received(self, data)
self.write(data)
self.finish()
所有幫助非常感謝。提前致謝!
謝謝你回答@hymloth!但是這個解決方案基本上會循環並不斷執行比較代碼 - 當用戶數量增長時,這不會導致性能問題嗎?如果是這樣,有沒有辦法在等待數據輸入的同時「掛起」請求? – MonkeyPetteri 2012-02-17 17:26:57
感謝您的更新@hymloth - 我正在考慮它,但似乎Firefox 3.x不支持websockets ..這是一個要求。任何其他想法? – MonkeyPetteri 2012-02-18 17:44:52