python - 多处理 [concurrent.futures.ProcessPoolExecutor] 未正确调用函数
问题描述
各位程序员,你好,我是多处理的新手。我看过这个关于多处理的指南:https ://www.youtube.com/watch?v=fKl2JW_qrso&t=2367s并试图让代码工作。基本上,它的作用是读取一堆图像并将它们保存到processed
文件夹中。
zip (130MB) 链接:https ://ufile.io/kt420x7q
整个代码:
import time
import concurrent.futures
from pathlib import Path
from PIL import Image, ImageFilter
parent_path = Path(__file__).parent
output_path = parent_path.joinpath('processed')
def process_image(img_name):
img = Image.open(img_name)
img = img.filter(ImageFilter.GaussianBlur(15))
img.thumbnail(size)
img.save(f'{output_path.joinpath(img_name)}')
print(f'{img_name} was processed...')
def main():
img_names = [
'photo-1532009324734-20a7a5813719.jpg',
'photo-1524429656589-6633a470097c.jpg',
'photo-1530224264768-7ff8c1789d79.jpg',
'photo-1541698444083-023c97d3f4b6.jpg',
'photo-1522364723953-452d3431c267.jpg',
'photo-1513938709626-033611b8cc03.jpg',
'photo-1493976040374-85c8e12f0c0e.jpg',
'photo-1504198453319-5ce911bafcde.jpg',
'photo-1530122037265-a5f1f91d3b99.jpg',
'photo-1516972810927-80185027ca84.jpg',
'photo-1550439062-609e1531270e.jpg']
t1 = time.perf_counter()
size = (1200, 1200)
## # Normal Loop Function
## for img_name in img_names:
## img = Image.open(img_name)
## img = img.filter(ImageFilter.GaussianBlur(15))
## img.thumbnail(size)
## img.save(f'{output_path.joinpath(img_name)}')
## print(f'{img_name} was processed...')
# Multiprocessing Function
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()
##input()
如您所见,有 aNormal Loop Function
和 a Multiprocessing Function
。MP-function
应该做与 完全相同的事情,但Normal Loop Function
它似乎无法process_image
正确执行功能。
当我运行时:
# Normal Loop Function
for img_name in img_names:
img = Image.open(img_name)
img = img.filter(ImageFilter.GaussianBlur(15))
img.thumbnail(size)
img.save(f'{output_path.joinpath(img_name)}')
print(f'{img_name} was processed...')
它按预期工作,打印进度并将图像保存到processed
文件夹:
photo-1532009324734-20a7a5813719.jpg was processed...
photo-1524429656589-6633a470097c.jpg was processed...
photo-1530224264768-7ff8c1789d79.jpg was processed...
photo-1541698444083-023c97d3f4b6.jpg was processed...
photo-1522364723953-452d3431c267.jpg was processed...
photo-1513938709626-033611b8cc03.jpg was processed...
photo-1493976040374-85c8e12f0c0e.jpg was processed...
photo-1504198453319-5ce911bafcde.jpg was processed...
photo-1530122037265-a5f1f91d3b99.jpg was processed...
photo-1516972810927-80185027ca84.jpg was processed...
photo-1550439062-609e1531270e.jpg was processed...
Finished in 11.8887978 seconds
但是当我尝试运行时MP-Function
:
# Multiprocessing Function
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(process_image, img_names)
输出是:
Finished in 3.8939473 seconds
在这种情况下,根本不会打印进度,也不会将图像保存到processed
文件夹中。此输出来自在 IDLE 中执行代码。由于 IDLE 似乎存在多处理问题,我还尝试通过从 Windows 终端运行脚本来执行该脚本。在这种情况下,没有输出,进程会无限期地运行。
解决方案
推荐阅读
- r - 将 purrr 和 ggplot 与 group_by 和 nest() 一起使用时获取绘图的标题
- java - 如何使用 java 在 selenium 中单击此按钮
- android - Android Room 关系在单独的表格中
- c# - 如何绕过模式干扰按钮单击事件?
- react-native - 如何导航到特定屏幕并禁止使用 react-native-navigation 返回
- r - 作为列表提供的参数未得到正确评估
- sql - 如何将 SQL Server 列转换为行?
- symfony-2.8 - JWT Token 总是返回 Not Found Symfony2.8
- hadoop - 如何在 Hadoop Streaming 中处理 2 个具有不同输入格式的文件?
- kubernetes - 如何避免在 kubernetes http liveness 和 readiness 探针中编码?