File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff 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+
3879API
3980---
4081
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ flake8==3.3.0
44pdfminer.six == 20170419
55pycodestyle == 2.3.1
66pyflakes == 1.5.0
7+ Pygments == 2.2.0
78pytest == 3.0.7
89pytest-cov == 2.5.0
910pytest-isort == 0.1.0
You can’t perform that action at this time.
0 commit comments