为什么ThreadPoolExecutor工作,而ProcessPoolExecutor不工作?(已解决)

2024-06-02 08:03:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python3中的线程和多处理。我尝试了一个使用ThreadPoolExecutor的简单程序,但当我改为ProcessPoolExecutor时,它没有执行给定的任何代码

from PIL import Image
from PIL import ImageFilter
import os

directory = r'C:\Users\admin\OneDrive\Pictures\My Photos'
imgs_file = []


for img_file in os.listdir(directory):
    if (img_file.endswith(".JPG") or img_file.endswith(".jpg")):
        imgs_file.append(img_file)

def image_filter(image_file):
    img = Image.open(image_file)
    img= img.filter(ImageFilter.GaussianBlur(radius = 9))
    img.save(f'Converted {image_file}')  

import concurrent.futures
with concurrent.futures.ProcessPoolExecutor() as p:
    p.map(image_filter, imgs_file)

Tags: fromimageimportimgpilosfilterconcurrent