在 python 中使用线程而不是进程的问题
problem with using threads instead of processes in python
所以我在 YouTube 上找到了 Corey Schafer 的多处理和线程教程。
在多处理视频中,我们有 15 张照片要对其添加模糊效果,并将它们添加到现有文件夹中名为 'processed' 的不同文件夹中。
import time
import concurrent.futures
from PIL import Image, ImageFilter
img_names = [
'photo-1516117172878-fd2c41f4a759.jpg',
'photo-1532009324734-20a7a5813719.jpg',
'photo-1524429656589-6633a470097c.jpg',
'photo-1530224264768-7ff8c1789d79.jpg',
'photo-1564135624576-c5c88640f235.jpg',
'photo-1541698444083-023c97d3f4b6.jpg',
'photo-1522364723953-452d3431c267.jpg',
'photo-1513938709626-033611b8cc03.jpg',
'photo-1507143550189-fed454f93097.jpg',
'photo-1493976040374-85c8e12f0c0e.jpg',
'photo-1504198453319-5ce911bafcde.jpg',
'photo-1530122037265-a5f1f91d3b99.jpg',
'photo-1516972810927-80185027ca84.jpg',
'photo-1550439062-609e1531270e.jpg',
'photo-1549692520-acc6669e2f0c.jpg'
]
def process_image(img_name):
size = (1200,1200)
img = Image.open(img_name)
img = img.filter(ImageFilter.GaussianBlur(15))
img.thumbnail(size)
img.save(f'processed/{img_name}')
print(f"{img_name} was processed")
def main():
t1 = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(process_image,img_names)
t2 = time.perf_counter()
print(f"finished in {t2-t1} seconds")
if __name__ == "__main__":
main()
问题是当我尝试使用
with concurrent.futures.ThreadPoolExecutor() as executor:
而不是我现在看到线程需要多长时间,然后只有一半的图片被处理并添加到新文件夹,一切正常
with concurrent.futures.ProcessPoolExecutor() as executor:
但没有线程。有人可以解释为什么吗?
顺便说一句,所有照片都保存在我现在用 python 文件打开的同一个文件夹中。
我设法通过在池中添加一个 max_workers arg 来解决它,同样,有人知道为什么需要这样做吗?我认为如果我不给它一个值,那么池将处理它并为我的系统使用尽可能多的值。
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
如果我尝试使用过多的线程或进程,我的 CPU 无法处理,我 运行 i7 4790K(4 核,8 线程)CPU 所以每当我尝试使用超过 8 个线程或处理程序停止处理下一张照片,在 运行 大约一半之后
所以我在 YouTube 上找到了 Corey Schafer 的多处理和线程教程。 在多处理视频中,我们有 15 张照片要对其添加模糊效果,并将它们添加到现有文件夹中名为 'processed' 的不同文件夹中。
import time
import concurrent.futures
from PIL import Image, ImageFilter
img_names = [
'photo-1516117172878-fd2c41f4a759.jpg',
'photo-1532009324734-20a7a5813719.jpg',
'photo-1524429656589-6633a470097c.jpg',
'photo-1530224264768-7ff8c1789d79.jpg',
'photo-1564135624576-c5c88640f235.jpg',
'photo-1541698444083-023c97d3f4b6.jpg',
'photo-1522364723953-452d3431c267.jpg',
'photo-1513938709626-033611b8cc03.jpg',
'photo-1507143550189-fed454f93097.jpg',
'photo-1493976040374-85c8e12f0c0e.jpg',
'photo-1504198453319-5ce911bafcde.jpg',
'photo-1530122037265-a5f1f91d3b99.jpg',
'photo-1516972810927-80185027ca84.jpg',
'photo-1550439062-609e1531270e.jpg',
'photo-1549692520-acc6669e2f0c.jpg'
]
def process_image(img_name):
size = (1200,1200)
img = Image.open(img_name)
img = img.filter(ImageFilter.GaussianBlur(15))
img.thumbnail(size)
img.save(f'processed/{img_name}')
print(f"{img_name} was processed")
def main():
t1 = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(process_image,img_names)
t2 = time.perf_counter()
print(f"finished in {t2-t1} seconds")
if __name__ == "__main__":
main()
问题是当我尝试使用
with concurrent.futures.ThreadPoolExecutor() as executor:
而不是我现在看到线程需要多长时间,然后只有一半的图片被处理并添加到新文件夹,一切正常
with concurrent.futures.ProcessPoolExecutor() as executor:
但没有线程。有人可以解释为什么吗?
顺便说一句,所有照片都保存在我现在用 python 文件打开的同一个文件夹中。
我设法通过在池中添加一个 max_workers arg 来解决它,同样,有人知道为什么需要这样做吗?我认为如果我不给它一个值,那么池将处理它并为我的系统使用尽可能多的值。
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
如果我尝试使用过多的线程或进程,我的 CPU 无法处理,我 运行 i7 4790K(4 核,8 线程)CPU 所以每当我尝试使用超过 8 个线程或处理程序停止处理下一张照片,在 运行 大约一半之后