Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于multiprocessing.pool.ThreadPool.imap_无序使用的问题_Python_Multithreading_Threadpool - Fatal编程技术网

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