Skip to Content
NexPDF

Platform notes

Threading, multiprocessing, memory, packaging and deployment notes for NexPDF for Python on servers and desktops.

English

Threading

  • A Document is not thread-safe. Use each document from one thread at a time.
  • Different documents can be used on different threads at the same time, so threads scale across cores for independent files. The state shared between documents (the license and the renderer's caches) is thread-safe.
  • doc.copy() gives an independent document you can hand to another thread.
  • The license is process-wide: one activate_license call covers every thread.
  • Errors are tracked per thread, so an error raised on one thread never leaks into another.

ctypes releases the GIL while a native call runs, so rendering or converting in a ThreadPoolExecutor runs in parallel:

from concurrent.futures import ThreadPoolExecutor
import nexpdf

def render_first_page(path: str) -> bytes:
    with nexpdf.Document.open(path) as doc:      # one document per task
        return doc.render_page_png(0, 144)

with ThreadPoolExecutor(max_workers=8) as pool:
    pngs = list(pool.map(render_first_page, paths))

Multiple processes

With multiprocessing, Gunicorn, uWSGI or Celery, each worker process loads its own copy of the engine and needs its own activate_license call. Activate in the worker start-up hook (for example Gunicorn's post_fork or Celery's worker_process_init), not only in the parent. Documents cannot be pickled; pass file paths or bytes between processes.

Memory

  • A document is read fully into memory when it is opened. Budget roughly the file size plus the decoded pages you touch.
  • A rendered page at 144 dpi of an A4 page is about 1190 x 1684 pixels before PNG compression. Render large documents page by page instead of keeping every PNG.
  • The renderer keeps process-wide caches of parsed fonts, glyphs and decoded images, shared by every document so a logo or font used in many files is decoded once. Each cache is bounded by size and drops its least recently used entries first; together they can hold up to about 1.3 GB on a process that renders many large, image-heavy files. Memory that stays high after a rendering batch is this cache being reused, not a leak.
  • Close documents with with or close(). They are also freed when garbage-collected, but not at a predictable time.
  • memory_usage_bytes() and trim_memory() let long-lived documents give memory back; see Compare and optimise.

Packaging and size

  • The package itself is pure Python. The engine is one shared library per platform: libnexpdf_ffi.so, libnexpdf_ffi.dylib or nexpdf_ffi.dll, about 20 MB for one architecture (the macOS library is universal and holds two).
  • The default engine compiles in the Liberation fonts (about 4 MB) used for non-embedded standard fonts and PDF/A. nexpdf.has_bundled_fonts() tells you which build you have.
  • For self-contained deployments, build one wheel per platform with the library in nexpdf/lib/, or set NEXPDF_LIB_DIR in your container image. See Installation.
  • PyInstaller and similar tools do not detect the library automatically. Add it as a binary (--add-binary "libnexpdf_ffi.so:nexpdf/lib") or set NEXPDF_LIB_PATH at run time.

Serverless

Container and function runtimes work like any Linux x86_64 or arm64 host, as long as the image has glibc 2.35 or later (see Troubleshooting). Ship the library in your deployment package or image, set NEXPDF_LIB_PATH, and activate the license once at module import so warm invocations reuse it. Use /tmp for intermediate files, or work with from_bytes and save_bytes to stay in memory.

Network

The engine makes no network calls of its own: license keys are verified offline and documents never leave the process. Only signing options you set (tsa_url for a timestamp, ltv for OCSP and CRL lookups) and add_ltv contact the servers named in the certificates.

On this page