Support for an Interruptible Library
Overview
Aspose.Slides provides an interruptible processing mechanism for long-running presentation tasks, such as deserialization, serialization, and rendering. This mechanism is based on the InterruptionToken and InterruptionTokenSource classes.
An InterruptionToken can be assigned to LoadOptions and passed to the Presentation constructor. When InterruptionTokenSource.interrupt is called, the associated long-running task is interrupted.
Interruptible Library
Aspose.Slides for Python via Java provides the InterruptionToken and InterruptionTokenSource classes. They allow you to interrupt long-running tasks such as deserialization, serialization, and rendering.
- InterruptionTokenSource is the source of the token(s) passed to LoadOptions.setInterruptionToken.
- When LoadOptions.setInterruptionToken is called and the LoadOptions instance is passed to the Presentation constructor, invoking InterruptionTokenSource.interrupt interrupts any long-running task associated with that Presentation.
The following code snippet demonstrates interrupting a running task:
from concurrent.futures import ThreadPoolExecutor
import time
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import InterruptionTokenSource, LoadOptions, Presentation, SaveFormat
token_source = InterruptionTokenSource()
def convert_presentation():
load_options = LoadOptions()
load_options.setInterruptionToken(token_source.getToken())
presentation = Presentation("sample.pptx", load_options)
try:
presentation.save("sample.ppt", SaveFormat.Ppt)
finally:
presentation.dispose()
with ThreadPoolExecutor(max_workers=1) as executor:
conversion_task = executor.submit(convert_presentation) # Run the action in a separate thread.
time.sleep(10) # Timeout.
token_source.interrupt() # Stop the conversion.
conversion_task.result()
FAQ
What is the purpose of the Aspose.Slides interrupt library?
It provides a mechanism to interrupt long-running operations—such as loading, saving, or rendering presentations—before they complete. This is useful when processing time must be limited or the task is no longer needed.
What is the difference between InterruptionToken and InterruptionTokenSource?
- InterruptionToken is passed to the Aspose.Slides API and checked during long-running operations.
- InterruptionTokenSource is used in your code to create tokens and trigger interruptions by calling interrupt.
What tasks can be interrupted?
Any Aspose.Slides task that accepts an InterruptionToken—such as loading a presentation with Presentation or saving with Presentation.save—can be interrupted.
Does interruption happen immediately?
No. Interruption is cooperative: the operation periodically checks the token and stops as soon as it detects that interrupt has been called.
What happens if I call interrupt after a task has already completed?
Nothing—the call has no effect if the corresponding task has already completed.
Can I reuse the same InterruptionTokenSource for multiple tasks?
Yes—but after you call interrupt on that source, all tasks using its tokens will be interrupted. Use separate token sources to manage tasks independently.