【发布时间】:2014-03-09 13:01:22
【问题描述】:
当我的 GAE 服务器与 EC2 REST 服务器通信时,我遇到了这个 60 秒超时问题。在GAE 方面,我的任务是上传csv 文件,解析其信息并将每一行作为请求发送到REST 服务器。我正在使用多线程和任务队列来减少请求时间,但当文件很大时仍然超时。以下是我的代码示例,我很感激任何建议。
from threading import Thread
import Queue
thread_count = 10 #the number of theading
job_q = Queue.Queue() #a job queue
def html_table(row_inp_all):
while True:
row_inp_temp_all = row_inp_all.get()
all_dic = {"row_inp_temp_all": row_inp_temp_all}
data = json.dumps(all_dic)
url=url_part1 + '/przm/' + jid
response = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=http_headers, deadline=60)
output_val = json.loads(response.content)['result']
def loop_html(thefile):
reader = csv.reader(thefile.file.read().splitlines())
header = reader.next()
for row in reader:
job_q.put(row)
all_threads = [Thread(target=html_table, args=(job_q, )) for j in range(thread_count)]
for x in all_threads:
x.start()
for x in all_threads:
job_q.put(None)
for x in all_threads:
x.join()
【问题讨论】:
-
你能在后台做吗?如果是这样,谷歌应用引擎任务队列可能就是票:developers.google.com/appengine/docs/python/taskqueue
-
@mgilson:感谢您的建议!
-
很少需要在 appengine 上使用线程,如果代码没有等待资源,它可能会运行得更慢。异步方法和任务队列是您可能需要的全部。
标签: python google-app-engine timeout urlfetch