Python 关于multiprocessing.pool.ThreadPool.imap_无序使用的问题
下面是一个示例代码:Python 关于multiprocessing.pool.ThreadPool.imap_无序使用的问题,python,multithreading,threadpool,Python,Multithreading,Threadpool,下面是一个示例代码: def process_image(args): try: //Code section except: print('Error:', img_filepath) img_filepaths = get_img_filepaths(images_dir) n_threads = max(min(multiprocessing.cpu_count(), len(img_filepaths)), 1) pool = Thre
def process_image(args):
try:
//Code section
except:
print('Error:', img_filepath)
img_filepaths = get_img_filepaths(images_dir)
n_threads = max(min(multiprocessing.cpu_count(), len(img_filepaths)), 1)
pool = ThreadPool(n_threads)
input_args = zip(img_filepaths, itertools.repeat(output_dir))
for _ in tqdm(pool.imap_unordered(process_image, input_args), total=len(img_filepaths)):
pass
pool.close()
pool.join()
是否每次只有一个线程访问//代码节
?如果我在这个代码段中使用某个资源(每次只能由一个线程访问),我是否需要锁定它?该锁是全局锁(在.py文件顶部定义)还是直接传递给process\u image