I'm trying to write simple multi-threaded python script:
from multiprocessing.dummy import Pool as ThreadPool
def resize_img_folder_multithreaded(img_fldr_src,img_fldr_dst,max_num_of_thread):
images = glob.glob(img_fldr_src+'/*.'+img_file_extension)
pool = ThreadPool(max_num_of_thread)
pool.starmap(resize_img,zip(images,itertools.repeat(img_fldr_dst)))
# close the pool and wait for the work to finish
pool.close()
pool.join()
def resize_img(img_path_src,img_fldr_dest):
#print("about to resize image=",img_path_src)
image = io.imread(img_path_src)
image = transform.resize(image, [300,300])
io.imsave(os.path.join(img_fldr_dest,os.path.basename(img_path_src)),image)
label = img_path_src[:-4] + '.xml'
if copyLabels is True and os.path.exists(label) is True :
copyfile(label,os.path.join(img_fldr_dest,os.path.basename(label)))
setting the argument max_num_of_thread
to any number in [1...10]
doesn't improve my run time at all (for 60 images it stays around 30 sec
) , the max_num_of_thread
=10 my PC got stuck
my question is : what is the bottle neck in my code , why can't I see any improvement?
some data about my PC :
python -V
Python 3.6.4 :: Anaconda, Inc.
cat /proc/cpuinfo | grep 'processor' | wc -l
4
cat /proc/meminfo
MemTotal: 8075960 kB
MemFree: 3943796 kB
MemAvailable: 4560308 kB
cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=17.10