On Tue, 2014-06-10 at 10:17 -0400, Chris McDonough wrote: > You might be able to use the "get_connection()" mechanism from > pyramid_zodbconn. But in reality it's going to be dicey, because the > app_iter may be processed outside the thread that handled the HTTP > request.
Actually, for our particular use-case, this is not a concern. The
data-set is very small. So, we went ahead by generating an intermediate
file and passing it off to pyramid.response.FileIter.
However, was curious on how to solve this for good (for the case of huge
data-sets). The following seems to work with waitress:
@view_config(route_name="csv_export")
def csv_export(request):
zodb_dbs = request.registry._zodb_databases
primary_db = zodb_dbs.get('')
csv_iter = CSVIter(primary_db)
res = request.response
res.content_type = "text/csv; name=report.csv"
res.content_disposition = "attachment; filename=report.csv"
res.app_iter = csv_iter
return res
class CSVIter(object):
def __init__(self, db):
self.db = db
self.conn = None
self.entries = None
self.index = 0
def __iter__(self):
self.conn = self.db.open()
root = appmaker(self.conn.root())
self.entries = root['users'].values()
# 'users' is a OOBTree
return self
def next(self):
if self.index >= len(self.entries):
self.conn.close()
raise StopIteration
ent = self.entries[self.index]
out = cStringIO.StringIO()
writer = csv.DictWriter(
out, ('name', 'id'), extrasaction='ignore')
writer.writerow(ent.to_dict())
self.index += 1
return out.getvalue()
__next__ = next # py3
def close(self):
pass
I dont see why the above should not work with other servers. Any thoughts on
this?
Joe
--
Joe Steeve
HiPro IT Solutions Private Limited
http://hipro.co.in/
signature.asc
Description: This is a digitally signed message part
