Skip to content

Instantly share code, notes, and snippets.

@winhamwr
Created March 12, 2010 17:24
Show Gist options
  • Save winhamwr/330547 to your computer and use it in GitHub Desktop.
Save winhamwr/330547 to your computer and use it in GitHub Desktop.
celery task using wait()
def build_document_pdf(document, realtime=False, timeout=None, watermark=True):
"""
Builds and caches a PDF copy of this document.
If ``realtime`` is True, returns the URL to the PDF. Otherwise, the PDF is
generated as asynchronously in the background.
"""
if not timeout:
timeout = print_settings.PRINTING_TIMEOUT
pdf_generator = DocumentPdfGenerator()
if realtime:
result = pdf_generator.delay(document, watermark=watermark)
try:
result.wait(timeout)
if not result.successful():
raise ValueError(result.result)
return result.result
except TimeoutError:
return reverse('printing_wait', kwargs={'task_id': result.task_id})
else:
return pdf_generator.delay(document, watermark=watermark)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment