在此如何避免超过128 mb的软内存限制为164 mb,以及如何使用taskqueue.add()为此代码提前。谢谢。
如果你有任何疑问我准备告诉你。
class ExportHandler(BaseHandler):
def get(self):
tables = []
app_models = [m for m in dir(models.app) if not m.startswith("__")]
admin_models = [m for m in dir(models.admin) if not m.startswith("__")]
total = app_models+admin_models
for mname in total:
if mname not in REMOVE:
label = title(mname)
tables.append({'value':mname, 'label':label})
fromdate = None
todate = None
if "fromdate" in self.request.GET:
fdate = self.request.GET['fromdate']
try:
fromdate = datetime.strptime(fdate, "%m/%d/%Y")
except Exception as e:
pass
if "todate" in self.request.GET:
tdate = self.request.GET['todate']
try:
todate = datetime.strptime(tdate, "%m/%d/%Y")
todate = todate+timedelta(days=1)
except Exception as e:
pass
if fromdate is not None and todate is not None:
table = self.request.GET['table']
model_class = get_class(table, app_models, admin_models)
model_name = model_class.__name__
columns = model_class.properties().keys()
query = model_class.gql("where created_date >= :1 and created_date <= :2", fromdate, todate)
if 'date_time' in columns:
query = model_class.gql("where date_time >= :1 and date_time <= :2", fromdate, todate)
records = [i for i in query.run()]
if bool(records):
fmt = "%m/%d/%Y"
filename = "%s_%s_%s" %(model_name, fromdate.strftime(fmt), todate.strftime(fmt))
csvfile = self.response
writer = csv.writer(csvfile, delimiter=",")
writer.writerow(records[0].json().keys())
for rec in records:
writer.writerow(rec.json().values())
self.response.content_disposition = 'attachment; filename=%s.csv' % filename
return self.response
else:
self.session.add_flash("No records found", key='error')
self.render_response("/admin/exportcsv/index.html", tables=tables)
def post(self):
print self.request.POST
self.render_response("/admin/exportcsv/index.html")