在excel文件生成期间,数据存储区操作超时

时间:2013-10-26 13:57:06

标签: python excel google-app-engine xlwt xlutils

我正在使用xlwt生成一个excel文件,并将其作为blob属性存储在数据存储区中。但是在这一代中出现了这个错误"Timeout: The datastore operation timed out, or the data was temporarily unavailable."

我注意到使用xlutils我们可以更新现有文件。我打算做这样的事情,第一次用一半的数据来填充文件,使用另一个任务完成文件创建。

有没有更好的方法呢?

这是我目前的代码:

from xlwt import *
wb = Workbook()
ws0 = wb.add_sheet("Sheet 1")

style = XFStyle()
style.font.name = 'Tahoma'   

currency_style = XFStyle()
currency_style.num_format_str = '$#,##0.00'       

ws0.write(0, 0, 'Col 1', style)
ws0.write(0, 1, 'Col 2', style)
ws0.write(0, 2, 'Col 3', style)
ws0.write(0, 3, 'Col 4', style)
rx = 1

for each in db_result:                
      ws0.write(rx, 0, each.col1, style)
      ws0.write(rx, 1, each.col2, style)
      ws0.write(rx, 2, each.col3, style)

      try:
          ws0.write(rx, 3, round(float(each.col4), 2), currency_style)
      except:
          ws0.write(rx, 3, each.col4, style)

      rx = rx + 1                
      db.delete(each)

buffer = StringIO.StringIO()
wb.save(buffer)
contents = buffer.getvalue()

f = myfile()
f.name = 'File 1'
f.file = db.Blob(contents)
f.put()

1 个答案:

答案 0 :(得分:1)

如果只是一个超时问题,你的代码运行时间更长,那么你可以使用一个任务代替超时,这会给你一个10分钟的超时而不是60秒:Background work with the deferred library

from google.appengine.ext import deferred

  def do_something_expensive(a, b, c=None):
      logging.info("Doing something expensive!")
      # Do your work here

  # Somewhere else
  deferred.defer(do_something_expensive, "Hello, world!", 42, c=True)

所以将程序包装在一个可以传递给defer方法的函数中。或者看看使用其他一些选项,如后端:

https://developers.google.com/appengine/docs/python/backends/

  

App引擎后端是您的应用程序免除的实例   请求截止日期并可访问更多内存(最大1GB)和CPU   (高达4.8GHz)比正常情况。它们是专为   需要更快性能,大量应用的应用程序   可寻址的内存,以及连续或长时间运行的背景   流程。后端有多种尺寸和配置,并且有   计算正常运行时间而非CPU使用率。