本文整理汇总了Python中multiprocessing.dummy.Pool.starmap_async方法的典型用法代码示例。如果您正苦于以下问题:Python Pool.starmap_async方法的具体用法?Python Pool.starmap_async怎么用?Python Pool.starmap_async使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.dummy.Pool
的用法示例。
在下文中一共展示了Pool.starmap_async方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from multiprocessing.dummy import Pool [as 别名]
# 或者: from multiprocessing.dummy.Pool import starmap_async [as 别名]
class ImageHandler:
dir_postfix = 'imgc'
dir_files = '_files-imgc'
imgs_done = 0
imgs_total = 0
def __init__(self, queue=None, **kwargs):
self.__dict__.update(kwargs)
self.images = [] # list of (src, dst) tuples
# threading.Thread.__init__(self)
# self.queue = queue
# self._stop = threading.Event()
self.finished = False
self.generate_image_paths()
def generate_image_paths(self):
for arg in self.src_images:
# process directory path
if os.path.isdir(arg):
src = os.path.abspath(arg)
dst = os.path.abspath("{}-{}".format(src, self.dir_postfix))
if not os.path.exists(dst):
os.makedirs(dst) # target path must always exist
for root, dirs, files in os.walk(src):
for d in dirs:
src_dir = os.path.join(root, d)
dst_dir = src_dir.replace(src, dst)
for f in files:
if extension(f) not in IMAGE_EXTS: continue
# if os.path.splitext(f)[1][1:].strip().lower() not in IMAGE_EXTS: continue
src_file = os.path.join(root, f)
dst_file = src_file.replace(src, dst)
parent_dir = os.path.dirname(dst_file)
print(parent_dir)
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
print(dst_file)
self.append((src_file, dst_file))
# process file path
elif os.path.isfile(arg):
src = os.path.abspath(arg)
dst_dir = os.path.join(os.path.dirname(src), self.dir_files)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
dst = os.path.join(dst_dir, os.path.basename(arg))
self.append((src, dst))
# print (self.images)
self.imgs_total = len(self.images)
# def stop(self):
# self._stop.set()
# def stopped(self):
# return self._stop.isSet()
def terminate_pool(self):
if not self.finished:
self.pool.close()
self.pool.terminate()
return self.finished
def on_finish(self, x):
print("{}: finished successfully!".format(self.__class__.__name__))
self.finished = True
self.error = False
def on_error(self, x):
raise x
# print("{}: error - {}".format(self.__class__.__name__, str(x)))
# self.finished = True
# self.error = True
def run(self):
if not self.images:
print("No images found!")
sys.exit()
# print("Images found: {}".format(self.imgs_total))
print("Images found: {}".format(len(self.images)))
# print(self.images)
self.pool = ThreadPool(self.workers)
time_start = time.time()
# self.pool.starmap(self.resize_image, self.images)
self.pool.starmap_async(
self.resize_image, self.images, 1,
self.on_finish, self.on_error)
# pool.close()
# pool.join()
# time_end = time.time()
# # window = tkinter.Tk()
# # window.wm_withdraw()
#.........这里部分代码省略.........