2012-11-02 35 views
1

這可能是一個基本的問題,但我一直未能找到答案。如何在長時間運行的過程中獲取Python任務隊列和通道API來發送消息並響應請求?

我有一個長期運行的過程,每隔幾分鐘就會產生一次數據,我希望客戶端一旦準備好就能收到數據。目前,我在任務隊列中有長時間運行的過程,並且它將通道消息從for循環中添加到另一個任務隊列中。客戶端使用獲取請求成功接收頻道消息並下載數據;但是,消息是在長時間運行過程完成後(大約10分鐘後)從任務隊列發送的,而不是消息添加到任務隊列的時間。
如何讓任務隊列中的消息立即發送?我是否需要將for循環分解爲幾個任務? for循環創建了大量字典,我認爲我需要將其發佈到數據存儲中,然後爲下一次迭代檢索(似乎不是理想的解決方案),除非有更簡單的方法從任務返回數據。

當我沒有將消息添加到任務隊列並直接在for循環中發送消息時,服務器似乎沒有響應客戶端的數據獲取請求(可能是由於for循環的長? - 運行過程阻斷響應)

這裏是我的服務器代碼的簡化版本:

from google.appengine.ext import db 
from google.appengine.api import channel 
from google.appengine.api import taskqueue 
from google.appengine.api import rdbms 

class MainPage(webapp2.RequestHandler): 
def get(self): 
## This opens the GWT app  

class Service_handler(webapp2.RequestHandler): 
def get(self, parameters): 
## This is called by the GWT app and generates the data to be 
## sent to the client. 
    #This adds the long-process to a task queue 
    taskqueue.Task(url='/longprocess/', params = {'json_request': json_request}).add(queue_name='longprocess-queue') 

class longprocess_handler(webapp2.RequestHandler): 
def post(self): 
    #This has a for loop that recursively uses data in dictionaries to 
    #produce kml files every few minutes 
    for j in range(0, Time): 
     # Process data 
     # Send message to client using a task queue to send the message. 
     taskqueue.Task(url='/send/', params).add(queue_name=send_queue_name) 

class send_handler(webapp2.RequestHandler): 
def post(self): 
    # This sends the message to the client 
    # This is currently not happening until the long-process finishes, 
    # but I would like it to occur immediately. 


class kml_handler(webapp2.RequestHandler): 
def get(self, client_id): 
## When the client receives the message, it picks up the data here. 

app = webapp2.WSGIApplication([ 
         webapp2.Route(r'/', handler=MainPage), 
         webapp2.Route(r'/Service/', handler=Service_handler), 
         webapp2.Route(r'/_ah/channel/<connected>/', handler = connection_handler), 
         webapp2.Route(r'/longprocess/', handler = longprocess_handler), 
         webapp2.Route(r'/kml/<client_id>', handler = kml_handler), 
         webapp2.Route(r'/send/', handler = send_handler) 
         ], 
         debug=True) 

我需要長期的過程分解成該發送任務,並從數據存儲結果爲了讓send_handler立即執行,還是錯過了某些東西?謝謝

+1

您是否在SDK中看到這種行爲,或者一旦部署到App Engine本身? SDK一次只能處理一件事情,而通道API的行爲則不同。 – Greg

+0

這在App Engine SDK中的開發模式中發生。所以有可能在生產模式下這些任務隊列可能同時工作? – dave

回答

2

App Engine開發服務器一次只處理一個請求。在生產中,這些事情將同時發生。嘗試製作,並檢查事情是否如預期那樣行事。

在生產中使用單獨的任務發送通道消息也沒什麼必要 - 只需從主任務直接發送即可。

+0

謝謝!我會在生產模式下嘗試這個。 – dave

相關問題