Aspose.Slides for Python via Java 的多线程
介绍
虽然可以并行处理演示文稿(除了解析、加载和克隆之外),且通常表现良好,但在多个线程中使用该库时仍有出现错误结果的微小可能性。
我们强烈建议您 不要 在多线程环境中使用单个 Presentation 实例,因为这可能导致难以检测的不可预期错误或故障。
在多个线程中加载、保存和/或克隆 Presentation 实例是 不安全 的。这类操作 不受支持。如果需要执行此类任务,必须使用多个单线程进程并行化操作——每个进程应使用其自己的演示文稿实例。
并行将演示文稿幻灯片转换为图像
假设我们希望并行地将 PowerPoint 演示文稿的所有幻灯片转换为 PNG 图像。由于在多个线程中使用单个 Presentation 实例是不安全的,我们将演示文稿的幻灯片拆分为多个独立的演示文稿,并在各自的线程中并行地将幻灯片转换为图像。以下代码示例展示了实现方法。
from concurrent.futures import ThreadPoolExecutor
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ImageFormat, Presentation, SlideSizeScaleType
input_file_path = "sample.pptx"
output_file_path_template = "slide_{}.png"
image_scale = 2.0
def convert_slide_to_image(slide_presentation, slide_number):
try:
slide = slide_presentation.getSlides().get_Item(0)
image = slide.getImage(image_scale, image_scale)
try:
image_file_path = output_file_path_template.format(slide_number)
image.save(image_file_path, ImageFormat.Png)
finally:
image.dispose()
finally:
slide_presentation.dispose()
presentation = Presentation(input_file_path)
try:
slide_count = presentation.getSlides().size()
slide_size = presentation.getSlideSize().getSize()
slide_width = jpype.JFloat(slide_size.getWidth())
slide_height = jpype.JFloat(slide_size.getHeight())
with ThreadPoolExecutor() as executor:
conversion_tasks = []
for slide_index in range(slide_count):
# 将幻灯片提取到单独的演示文稿中。
slide_presentation = Presentation()
slide_presentation.getSlideSize().setSize(slide_width, slide_height, SlideSizeScaleType.DoNotScale)
slide_presentation.getSlides().removeAt(0)
slide_presentation.getSlides().addClone(presentation.getSlides().get_Item(slide_index))
# 在单独的任务中将幻灯片转换为图像。
slide_number = slide_index + 1
conversion_task = executor.submit(convert_slide_to_image, slide_presentation, slide_number)
conversion_tasks.append(conversion_task)
# 等待所有任务完成。
for conversion_task in conversion_tasks:
conversion_task.result()
finally:
presentation.dispose()
常见问题
我需要在每个线程中调用许可证设置吗?
不需要。只需在进程启动并在创建线程之前调用一次即可。如果 license setup 可能被并发调用(例如在懒加载期间),请对该调用进行同步,因为许可证设置方法本身不是线程安全的。
我可以在不同线程之间传递 Presentation 或 Slide 对象吗?
不建议在不同线程之间传递 “活跃的” 演示文稿对象:请为每个线程使用独立实例,或提前为每个线程创建单独的演示文稿或幻灯片容器。此做法遵循不在多线程之间共享单个演示文稿实例的一般建议。
如果每个线程都有自己的 Presentation 实例,将导出到不同格式(PDF、HTML、图像)并行化安全吗?
是的。只要使用独立的实例和各自的输出路径,此类任务通常能够正确并行化;请避免共享演示文稿对象和共享的 I/O 流。
在多线程环境中,我应如何处理全局字体设置(文件夹、替代)?
在启动线程之前初始化所有全局 font settings,并且在并行工作期间不要更改它们。这可以消除访问共享字体资源时的竞争。