这可能是一个基本问题,但我一直无法找到答案。
我有一个长时间运行的过程,每隔几分钟就会产生一次数据,我希望客户在准备好后立即接收这些数据。目前,我在任务队列中有一个长时间运行的进程,它从 for 循环中将通道消息添加到另一个任务队列。客户端成功接收到通道消息并使用get请求下载数据;但是,消息是在长时间运行的进程完成后(大约 10 分钟后)而不是在将消息添加到任务队列时从任务队列发送的。
如何立即发送任务队列中的消息?我需要将 for 循环分成几个任务吗?for 循环创建了许多字典,我认为我需要将其发布到数据存储中,然后为下一次迭代检索(似乎不是一个理想的解决方案),除非有更简单的方法从任务中返回数据。
当我没有将消息添加到任务队列并直接在for循环中发送消息时,服务器似乎没有响应客户端对数据的get请求(可能是由于长时间运行的进程阻塞的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 立即执行,还是我遗漏了什么?谢谢