Since version 26.11, a render can be stopped while it runs. Create a CancellationTokenSource, pass its token to view(), and cancel the source when the render should stop: from another thread with cancel(), or after a time limit with cancel_after(seconds). The call then raises OperationCanceledError. The pages written so far stay, and the Viewer can render again.
get_view_info(), get_attachments() and save_attachment() take a cancellation_token too.
Cancel after a time limit
fromgroupdocs.viewerimportCancellationTokenSource,OperationCanceledError,Viewerfromgroupdocs.viewer.optionsimportHtmlViewOptionsdefcancel_after_time_limit():source=CancellationTokenSource()# Cancel automatically after 60 seconds; source.cancel() stops it at oncesource.cancel_after(60)try:withViewer("sample.docx")asviewer:options=HtmlViewOptions.for_embedded_resources("cancel_after_time_limit/page_{0}.html")viewer.view(options,cancellation_token=source.token)print("rendered before the time limit")exceptOperationCanceledError:print("cancelled: the render took longer than 60 seconds")if__name__=="__main__":cancel_after_time_limit()
sample.docx is the sample file used in this example. Click here to download it.
Any thread can call source.cancel(): a Stop button, a request that was dropped, or, as here, a page function that has seen enough pages. The engine stops before the next page:
importiofromgroupdocs.viewerimportCancellationTokenSource,OperationCanceledError,Viewerfromgroupdocs.viewer.optionsimportPngViewOptionsdefcancel_after_two_pages():source=CancellationTokenSource()pages={}defcreate_page_stream(page_number):pages[page_number]=io.BytesIO()ifpage_number==2:# Any thread may cancel; here the render stops after page 2source.cancel()returnpages[page_number]try:withViewer("sample.docx")asviewer:viewer.view(PngViewOptions(create_page_stream),cancellation_token=source.token)print("rendered %d pages"%len(pages))exceptOperationCanceledError:print("cancelled after %d pages"%len(pages))if__name__=="__main__":cancel_after_two_pages()
sample.docx is the sample file used in this example. Click here to download it.
The engine (GroupDocs.Viewer for .NET 26.10, which this package wraps) looks at the token at two points:
Before rendering starts. A cancel during a long load takes effect when the load ends.
Between pages of a render of all pages to HTML, PNG or JPEG. It stops before the next page.
Nothing else is interrupted. A render to PDF, and a render of an explicit page list (viewer.view(options, token, [1, 2])), run to the end once rendering has started.
With a page list, the token comes first, as in .NET: viewer.view(options, token, [1, 2]), or use keywords: viewer.view(options, cancellation_token=token, page_numbers=[1, 2]). cancellation_token=None is the same as no token.
Stop a render the engine does not check
To put a hard time limit on a PDF render, or on any call, run it in a worker process and end the process when the time is up. Start the worker with multiprocessing.get_context("spawn"), not fork: see Threads, processes and web servers.
importmultiprocessingfromgroupdocs.viewerimportViewerfromgroupdocs.viewer.optionsimportPdfViewOptionsdefrender_to_pdf(source_path,output_path):withViewer(source_path)asviewer:viewer.view(PdfViewOptions(output_path))defrender_with_time_limit():# A new process, not a fork of this onecontext=multiprocessing.get_context("spawn")worker=context.Process(target=render_to_pdf,args=("sample.docx","render_with_time_limit/output.pdf"))worker.start()worker.join(timeout=120)# secondsifworker.is_alive():worker.terminate()worker.join()print("stopped: the render took longer than 120 seconds")else:print("rendered, exit code %d"%worker.exitcode)if__name__=="__main__":render_with_time_limit()
sample.docx is the sample file used in this example. Click here to download it.
The worker process starts its own .NET runtime, which takes a few seconds, so a pool of workers (concurrent.futures.ProcessPoolExecutor(mp_context=multiprocessing.get_context("spawn"))) suits many documents better than a process per document.
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.