Threads, processes and web servers

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

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.

from concurrent.futures import ThreadPoolExecutor

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import PdfViewOptions

def render_to_pdf(job):
    source_path, output_path = job
    # A new Viewer for each render, created in the thread that uses it
    with Viewer(source_path) as viewer:
        viewer.view(PdfViewOptions(output_path))
    return output_path

def render_in_threads():
    jobs = [("sample.docx", "render_in_threads/output_%d.pdf" % number) for number in range(1, 4)]
    with ThreadPoolExecutor(max_workers=3) as pool:
        for output_path in pool.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.

render_in_threads/output_1.pdf (719 KB)
render_in_threads/output_2.pdf (719 KB)
render_in_threads/output_3.pdf (719 KB)

Download full output

Worker processes: spawn, not fork

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.

import multiprocessing

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import PdfViewOptions

def render_to_pdf(job):
    source_path, output_path = job
    with Viewer(source_path) as viewer:
        viewer.view(PdfViewOptions(output_path))
    return output_path

def render_in_processes():
    jobs = [("sample.docx", "render_in_processes/output_%d.pdf" % number) for number in range(1, 4)]
    # "spawn" starts fresh processes; a fork of a process that has used GroupDocs cannot use it
    context = multiprocessing.get_context("spawn")
    with context.Pool(processes=2) as pool:
        for output_path in pool.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.

render_in_processes/output_1.pdf (719 KB)
render_in_processes/output_2.pdf (719 KB)
render_in_processes/output_3.pdf (719 KB)

Download full output

Safe:

  • threads, with one Viewer per render;
  • 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.