Skip to content

Commit 05dd613

Browse files
committed
adding async example to readme
1 parent 4b44454 commit 05dd613

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,47 @@ Basic Usage
3535
with open('google.pdf', 'w') as f:
3636
f.write(pdf)
3737
38+
Async Usage
39+
-----------
40+
41+
Generation of lots of documents with wkhtmltopdf can be slow as wkhtmltopdf can only generate one document
42+
per process. To get round this pydf uses python 3's asyncio ``create_subprocess_exec`` to generate multiple pdfs
43+
at the same time. Thus the time taken to spin up processes doesn't slow you down.
44+
45+
.. code:: python
46+
47+
async def go_async():
48+
count = 20
49+
apydf = AsyncPydf(max_processes=20)
50+
51+
async def gen(i_):
52+
pdf = await apydf.generate_pdf(
53+
html,
54+
title='Benchmark',
55+
author='Samuel Colvin',
56+
subject='Mock Invoice',
57+
page_size='A4',
58+
zoom='1.25',
59+
margin_left='8mm',
60+
margin_right='8mm',
61+
)
62+
print(f'{i_:03}: {len(pdf)}')
63+
file = OUT_DIR / f'output_{i_:03}.pdf'
64+
file.write_bytes(pdf)
65+
66+
coros = []
67+
for i in range(count):
68+
coros.append(gen(i))
69+
await asyncio.gather(*coros)
70+
return count
71+
72+
loop = asyncio.get_event_loop()
73+
loop.run_until_complete(go_async())
74+
75+
76+
See `benchmarks/run.py <https://github.com/tutorcruncher/pydf/blob/master/benchmark/run.py>`__
77+
for a full example.
78+
3879
API
3980
---
4081

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ flake8==3.3.0
44
pdfminer.six==20170419
55
pycodestyle==2.3.1
66
pyflakes==1.5.0
7+
Pygments==2.2.0
78
pytest==3.0.7
89
pytest-cov==2.5.0
910
pytest-isort==0.1.0

0 commit comments

Comments
 (0)