Python: Multiprocessing with pool.Map,而main仍在工作 &g



我学习python有几天了,想了解一下这个。我正在做一个文件资源管理器,并希望加快缩略图的创建。看了一堆关于多进程的教程,但是没有一个显示在进程运行时如何继续main()。我需要结果。

import os
from multiprocessing import Pool
import image

folder='G:+++CODING+++\test3\'

def process_file(filepath):
return image.convert_to_bytes(filepath, resize=(100, 100))

def create_filepaths(folder):
filepaths = []
for file in os.scandir(folder):
filepaths.append(os.path.join(folder, file))
return filepaths

def main():
def process1():
print('process1 start')
pool = Pool()
return pool.map(process_file, create_filepaths(folder), chunksize=1)
process1()
while True:
# do stuff

if __name__ == '__main__':
main()

我尝试过下面的东西,结合多处理,但我不能在maininguilloop中调用process1,当然,在那里做我的主逻辑,因为循环只是一个线程。我只能做简单的GuiUpdates。这个问题只是为了理解和学习,我的最终实现必须不同。

import os
import time
from multiprocessing import Pool
import image
from time import sleep, perf_counter
import threading

start_time = perf_counter()
folder='G:+++CODING+++\test3\'

def process_file(filepath):
return image.convert_to_bytes(filepath, resize=(100, 100))

def create_filepaths(folder):
filepaths = []
for file in os.scandir(folder):
filepaths.append(os.path.join(folder, file))
return filepaths

def main():
def process1():
print('process1 start')
pool = Pool()
return pool.map(process_file, create_filepaths(folder), chunksize=1)
def thread_create():
return threading.Thread(target=MainGuiLoop)
def MainGuiLoop():
while True:
time.sleep(0.5)
print("-")
thread1 = thread_create()
thread1.start()
result1= process1()
result2= process1()
if __name__ == '__main__':
main()

附加问题:据我所知。map在完成之前是不可访问的。事实上,我需要它不时地更新缩略图,所以我必须使用.map_async(),它解决了无效的main(),但给出了无序的结果。但我可以用id创建列表,然后按块排序,然后在屏幕上显示。这是正确的方法吗?

文档建议您正在寻找进程池执行器。下面的代码来自链接。对于无序的结果,尝试传入一个有序的列表。

import concurrent.futures
import math
PRIMES = [
112272535095293,
112582705942171,
112272535095293,
115280095190773,
115797848077099,
1099726899285419]
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True
def main():
with concurrent.futures.ProcessPoolExecutor() as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
if __name__ == '__main__':
main()
所以,对于你的代码:
import os
import concurrent.futures
import image
FOLDER = 'G:+++CODING+++\test3\'
def process_file(filepath):
return image.convert_to_bytes(filepath, resize=(100, 100))
def create_filepaths(folder):
return sorted([os.path.join(folder, f) for f in os.scandir(folder)])
def main():
with concurrent.futures.ProcessPoolExecutor() as executor:
filepaths = create_filepaths(FOLDER)
executor.map(process_file, filepaths) 
while True:
# do stuff
if __name__ == '__main__':
main()

相关内容

最新更新