The rendering engine runs in your Python process, on the .NET runtime that the package embeds. Threads share that runtime, and work well. Processes need care: .NET does not survive fork(), so a process forked from one that has already used GroupDocs cannot use it.
One Viewer per render
Create a new Viewer for each render, in the thread that uses it, and close it when the render is done (the with statement does). Do not share a Viewer between threads, and do not render the same document twice with one Viewer: a workbook rendered a second time by the same Viewer comes out with more pages than the first time, a known issue of the engine.
Renders in different threads run in parallel, and each keeps its own output, including the io.BytesIO objects of rendering to memory.
fromconcurrent.futuresimportThreadPoolExecutorfromgroupdocs.viewerimportViewerfromgroupdocs.viewer.optionsimportPdfViewOptionsdefrender_to_pdf(job):source_path,output_path=job# A new Viewer for each render, created in the thread that uses itwithViewer(source_path)asviewer:viewer.view(PdfViewOptions(output_path))returnoutput_pathdefrender_in_threads():jobs=[("sample.docx","render_in_threads/output_%d.pdf"%number)fornumberinrange(1,4)]withThreadPoolExecutor(max_workers=3)aspool:foroutput_pathinpool.map(render_to_pdf,jobs):print("rendered",output_path)if__name__=="__main__":render_in_threads()
sample.docx is the sample file used in this example. Click here to download it.
A process started with the spawn or forkserver start method begins fresh and starts its own .NET runtime, which takes a few seconds, so reuse the workers of a pool rather than starting a process per document.
importmultiprocessingfromgroupdocs.viewerimportViewerfromgroupdocs.viewer.optionsimportPdfViewOptionsdefrender_to_pdf(job):source_path,output_path=jobwithViewer(source_path)asviewer:viewer.view(PdfViewOptions(output_path))returnoutput_pathdefrender_in_processes():jobs=[("sample.docx","render_in_processes/output_%d.pdf"%number)fornumberinrange(1,4)]# "spawn" starts fresh processes; a fork of a process that has used GroupDocs cannot use itcontext=multiprocessing.get_context("spawn")withcontext.Pool(processes=2)aspool:foroutput_pathinpool.map(render_to_pdf,jobs):print("rendered",output_path)if__name__=="__main__":render_in_processes()
sample.docx is the sample file used in this example. Click here to download it.
uvicorn --workers N, and gunicorn without --preload: each worker imports the application itself;
multiprocessing.get_context("spawn") or "forkserver" (the default on Linux from Python 3.14), for example concurrent.futures.ProcessPoolExecutor(mp_context=multiprocessing.get_context("spawn")) on Python 3.7 and later;
forking before the first GroupDocs call.
Not safe, once the parent process has made a GroupDocs call:
the fork start method, the default on Linux before Python 3.14;
gunicorn --preload with an application that calls GroupDocs while it loads;
Celery’s prefork pool.
On Linux and macOS, such a child gets RuntimeError: The .NET runtime cannot be used in a process forked after it started ... at its first GroupDocs call, and the parent keeps working. Versions up to 26.9.0 instead crashed the parent process with a segmentation fault.
Time limits
A render can be cancelled between pages, and a worker process can be ended when a render takes too long: see Cancel rendering.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Ask AI
Ask anything about this page — the answer is based on its content.
Searching the docs and writing an answer…
Something went wrong while getting an answer. Please try again in a moment.