[reportlab-users] intermittent problem

Werner Thie werner at thieprojects.ch
Wed Jun 6 13:26:17 EDT 2012


Hi Jens

Not being useable from threads seems to be the #1 problem for replab
when used at higher production rates on servers.

I solved the problem in twisted with ampoule offering a webservice on
some local port which can be hammered with requests in parallel because
ampoule is forking out the work into its process pool, which isolates
the PDF production with z3c.rml nicely. Hope you can glean whatever you
need from this

HTH Werner

The code coming after this invocation snippet defines the whole AMP
production process pooling while serving a post with a JSON dict as
argument...

def render_POST(self, request):
def massageResult(res):
try:
if res['retcode'] == 200:
pdffile = open(res['tmpfile'], 'rb')
content = pdffile.read()
pdffile.close()
os.unlink(res['tmpfile'])
request.setHeader("content-type", "application/pdf")
except:
content = '<html><body>PDF error encounterede</body></html>'
return content

d = deferToAMPProcess(MakePDF, jargs=request.args["jargs"][0])
d.addCallback(massageResult)
return d

---------------------------------------------------------------------
import sys, os, datetime
import json

from tempfile import NamedTemporaryFile

from ampoule import AMPChild
import ampoule.pool

from twisted.protocols.amp import Command, Integer, String

from zope.pagetemplate.pagetemplate import PTRuntimeError

from z3c.rml import pagetemplate

jdefaults = """{
"Umlaute": "äöüïÄÖÜÏ",
"templdir": "context",
"templname": "context",
"dsname": "context"
}"""

class MakePDF(Command):
arguments = [
("jargs", String())
]
response = [
("retcode", Integer()),
("tmpfile", String())
]
errors = {
Exception: "Exception",
}

class PDFCreator(AMPChild):
"""
Process-based transformation of template into PDF
"""

def buildReport(self, jargs= '{}'):
try:
jpy = json.loads(jargs)
templdir = jpy['templdir']
dsname = jpy['dsname']
templname = jpy['templname']
savepath = os.getcwd()
#print 'savepath: ', savepath
os.chdir(os.path.join(os.getcwd(), 'templates', templdir))

#template is an RML page template
template = os.path.join(os.getcwd(), templname + '.mpt')
rmlPageTemplate = pagetemplate.RMLPageTemplateFile(template)
dataset = {} #a dict for passing stuff into the RML template
res = True
pdf = rmlPageTemplate(**dataset)

file = NamedTemporaryFile(mode='wb', delete=False, suffix='.pdf')
file.write(pdf)
os.chdir(savepath)
except:
os.chdir(savepath)
res = False
type, value, traceback = sys.exc_info()
raise
return res, file.name

def make_pdf(self, jargs):
"""
Create a PDF using the given parameters.
"""
res, tmpfile = self.buildReport(jargs=jargs)
return {
"retcode": 200,
"tmpfile": tmpfile
}

MakePDF.responder(make_pdf)

#this sets up the crucial reference to the process pool
ampoule.pool.pp = ampoule.pool.ProcessPool(ampChild=PDFCreator)

if __name__ == '__main__':
os.chdir('../../..')
try:
print 'cwd: ', os.getcwd()
pdfcreator = PDFCreator()
res, pdf = pdfcreator.buildReport(jdefaults)
print pdf
except:
pass
print 'cwd: ', os.getcwd()


On 6/6/12 4:33 PM, Jens Lundström wrote:

> I am using Django 1.4 / Python 2.7 / reportlab (open source version) to generate pdf.

>

> Things have worked really great and I do pdf generation (as in http requested returned/downloaded generated pdf file) within Django 1.3 but now upgraded to Django 1.4 and now starting to get problems. They seem to be 'random' as it works most of the time but now and again I get 'random' problems (as in I see there are exceptions once in a while in logs) but I have never been able to reproduce problems myself (things works most of the time), the reports are generated using static information so each time the same fix data is accessed and used to generate reports.

>

> What I get is the following type of errors that always happens in pairs

>

> Exception Value: ParaParser instance has no attribute '_seq'

> Exception Location: /home/tss/lib/python2.7/reportlab-2.5-py2.7-linux-i686.egg/reportlab/platypus/paraparser.py in _complete_parse, line 1061

>

> and then

>

> Exception Value: 'NoneType' object has no attribute 'close'

> Exception Location: /home/tss/lib/python2.7/reportlab-2.5-py2.7-linux-i686.egg/reportlab/lib/xmllib.py in close, line 521

>

> I have also seen this error on its own:

>

> Exception Value: list index out of range

> Exception Location: /home/tss/lib/python2.7/reportlab-2.5-py2.7-linux-i686.egg/reportlab/platypus/paraparser.py in _push, line 941

>

> Given that I am having a hard time reproducing this and not sure if problem on my side or reportlab - I am open for suggestions on how to troubleshoot this (could it be sync issue as in many generating conccurently or just a plain bug or...) ???

>

> / Jens

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at lists2.reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users




More information about the reportlab-users mailing list