【问题标题】:GAE (Python) Urlfetch 60 Second Timeout IssueGAE (Python) Urlfetch 60 秒超时问题
【发布时间】: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


【解决方案1】:

任何请求都有 60 秒的硬性限制,如果您想要更多的时间,您应该使用Task Queues API,它可以在后台运行长达 10 分钟,这对您的情况来说应该绰绰有余了。

在深入了解任务队列之前,您可以尝试background work with the deferred library,它实际上使用的是任务队列 API,但使用起来更容易。

【讨论】:

  • 谢谢,这个任务队列能很好地处理线程吗?或者我需要重做整个结构......
猜你喜欢
  • 2012-10-30
  • 2011-05-06
  • 2012-01-12
  • 2017-10-22
  • 2018-10-04
  • 2018-05-08
  • 1970-01-01
  • 2015-07-03
  • 2013-01-24
相关资源
最近更新 更多