知行编程网知行编程网  2022-03-30 07:00 知行编程网 隐藏边栏 |   抢沙发  1 
文章评分 0 次,平均分 0.0

    转自:caspar

整理:机器学习算法与Python学习

segmentfault.com/a/1190000000414339 

Python 在程序并行化方面多少有些声名狼藉。撇开技术上的问题,例如线程的实现和 GIL,我觉得错误的教学指导才是主要问题。常见的经典 Python 多线程、多进程教程多显得偏"重"。而且往往隔靴搔痒,没有深入探讨日常工作中最有用的内容。

传统的例子

简单搜索下"Python 多线程教程",不难发现几乎所有的教程都给出涉及类和队列的例子:

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> os <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> PIL <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> multiprocessing <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Pool <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> PIL <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Image<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />SIZE = (<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">75</span>,<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">75</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />SAVE_DIRECTORY = <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> thumbs </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">get_image_paths</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(folder)</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">return</span> (os.path.join(folder, f) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> f <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> os.listdir(folder) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> jpeg </span> <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> f)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">create_thumbnail</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(filename)</span>:</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im = Image.open(filename)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im.thumbnail(SIZE, Image.ANTIALIAS)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    base, fname = os.path.split(filename) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    save_path = os.path.join(base, SAVE_DIRECTORY, fname)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im.save(save_path)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> __name__ == <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> __main__ </span>:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    folder = os.path.abspath(<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> 11_18_2013_R000_IQM_Big_Sur_Mon__e10d1958e7b766c3e840 </span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    os.mkdir(os.path.join(folder, SAVE_DIRECTORY))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    images = get_image_paths(folder)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool = Pool()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool.map(creat_thumbnail, images)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool.close()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool.join()</p>

哈,看起来有些像 Java 不是吗?

我并不是说使用生产者/消费者模型处理多线程/多进程任务是错误的(事实上,这一模型自有其用武之地)。只是,处理日常脚本任务时我们可以使用更有效率的模型。

问题在于…

首先,你需要一个样板类; 
其次,你需要一个队列来传递对象; 
而且,你还需要在通道两端都构建相应的方法来协助其工作(如果需想要进行双向通信或是保存结果还需要再引入一个队列)。

worker 越多,问题越多

按照这一思路,你现在需要一个 worker 线程的线程池。下面是一篇 IBM 经典教程中的例子——在进行网页检索时通过多线程进行加速。

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#Example2.py</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;">   <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />A more realistic thread pool example <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />   </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> time <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> threading <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Queue <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> urllib2 <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">class</span> <span class="" style="max-width: 100%;">Consumer(threading.Thread)</span>:</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">__init__</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(self, queue)</span>:</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        threading.Thread.__init__(self)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        self._queue = queue <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">run</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(self)</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">while</span> <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">True</span>: <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            content = self._queue.get() <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> isinstance(content, str) <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">and</span> content == <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> quit </span>:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">break</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            response = urllib2.urlopen(content)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">print</span> <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> Bye byes! </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">Producer</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">()</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    urls = [<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org </span>, <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.yahoo.com </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.scala.org </span>, <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.google.com </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># etc.. </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    queue = Queue.Queue()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    worker_threads = build_worker_pool(queue, <span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">4</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    start_time = time.time()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># Add the urls to process</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> url <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> urls: <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        queue.put(url)  <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># Add the poison pillv</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> worker <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> worker_threads:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        queue.put(<span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> quit </span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> worker <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> worker_threads:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        worker.join()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">print</span> <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> Done! Time taken: {} </span>.format(time.time() - start_time)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">build_worker_pool</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(queue, size)</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    workers = []<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> _ <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> range(size):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        worker = Consumer(queue)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        worker.start() <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        workers.append(worker)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">return</span> workers<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> __name__ == <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> __main__ </span>:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    Producer()</p>

这段代码能正确的运行,但仔细看看我们需要做些什么:构造不同的方法、追踪一系列的线程,还有为了解决恼人的死锁问题,我们需要进行一系列的 join 操作。这还只是开始……

至此我们回顾了经典的多线程教程,多少有些空洞不是吗?样板化而且易出错,这样事倍功半的风格显然不那么适合日常使用,好在我们还有更好的方法。

何不试试 map

map 这一小巧精致的函数是简捷实现 Python 程序并行化的关键。map 源于 Lisp 这类函数式编程语言。它可以通过一个序列实现两个函数之间的映射。

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;">   urls = [<span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.yahoo.com </span>, <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.reddit.com </span>]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    results = map(urllib2.urlopen, urls)</p>

上面的这两行代码将 urls 这一序列中的每个元素作为参数传递到 urlopen 方法中,并将所有结果保存到 results 这一列表中。其结果大致相当于:

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;">results = []<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> url <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> urls: <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    results.append(urllib2.urlopen(url))</p>

map 函数一手包办了序列操作、参数传递和结果保存等一系列的操作。

为什么这很重要呢?这是因为借助正确的库,map 可以轻松实现并行化操作。

一行 Python 代码实现并行

在 Python 中有个两个库包含了 map 函数: multiprocessing 和它鲜为人知的子库 multiprocessing.dummy.

这里多扯两句: multiprocessing.dummy? mltiprocessing 库的线程版克隆?这是虾米?即便在 multiprocessing 库的官方文档里关于这一子库也只有一句相关描述。而这句描述译成人话基本就是说:"嘛,有这么个东西,你知道就成."相信我,这个库被严重低估了!

dummy 是 multiprocessing 模块的完整克隆,唯一的不同在于 multiprocessing 作用于进程,而 dummy 模块作用于线程(因此也包括了 Python 所有常见的多线程限制)。 
所以替换使用这两个库异常容易。你可以针对 IO 密集型任务和 CPU 密集型任务来选择不同的库。

动手尝试

使用下面的两行代码来引用包含并行化 map 函数的库:

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> multiprocessing <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Pool<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> multiprocessing.dummy <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Pool <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">as</span> ThreadPool</p>

实例化 Pool 对象:

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;">pool = ThreadPool()</p>

这条简单的语句替代了 example2.py 中 buildworkerpool 函数 7 行代码的工作。它生成了一系列的 worker 线程并完成初始化工作、将它们储存在变量中以方便访问。

Pool 对象有一些参数,这里我所需要关注的只是它的第一个参数:processes. 这一参数用于设定线程池中的线程数。其默认值为当前机器 CPU 的核数。

一般来说,执行 CPU 密集型任务时,调用越多的核速度就越快。但是当处理网络密集型任务时,事情有有些难以预计了,通过实验来确定线程池的大小才是明智的。

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;">pool = ThreadPool(<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">4</span>) <span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># Sets the pool size to 4</span></p>

线程数过多时,切换线程所消耗的时间甚至会超过实际工作时间。对于不同的工作,通过尝试来找到线程池大小的最优值是个不错的主意。

创建好 Pool 对象后,并行化的程序便呼之欲出了。我们来看看改写后的 example2.py

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> urllib2 <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> multiprocessing.dummy <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Pool <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">as</span> ThreadPool <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />urls = [<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org </span>, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org/about/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org/doc/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org/download/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org/getit/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org/community/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> https://wiki.python.org/moin/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://planet.python.org/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> https://wiki.python.org/moin/LocalUserGroups </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org/psf/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://docs.python.org/devguide/ </span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> http://www.python.org/community/awards/ </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># etc.. </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># Make the Pool of workers</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />pool = ThreadPool(<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">4</span>) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># Open the urls in their own threads</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># and return the results</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />results = pool.map(urllib2.urlopen, urls)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#close the pool and wait for the work to finish </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />pool.close() <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />pool.join()</p>

实际起作用的代码只有 4 行,其中只有一行是关键的。map 函数轻而易举的取代了前文中超过 40 行的例子。为了更有趣一些,我统计了不同方法、不同线程池大小的耗时情况。

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># results = [] </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># for url in urls:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#   result = urllib2.urlopen(url)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#   results.append(result)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># # ------- VERSUS ------- # </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># # ------- 4 Pool ------- # </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># pool = ThreadPool(4) </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># results = pool.map(urllib2.urlopen, urls)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># # ------- 8 Pool ------- # </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># pool = ThreadPool(8) </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># results = pool.map(urllib2.urlopen, urls)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># # ------- 13 Pool ------- # </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># pool = ThreadPool(13) </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;"># results = pool.map(urllib2.urlopen, urls)</span></p>

结果:

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#        Single thread:  14.4 Seconds </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#               4 Pool:   3.1 Seconds</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#               8 Pool:   1.4 Seconds</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(117, 113, 94);box-sizing: border-box !important;overflow-wrap: break-word !important;">#              13 Pool:   1.3 Seconds</span></p>

很棒的结果不是吗?这一结果也说明了为什么要通过实验来确定线程池的大小。在我的机器上当线程池大小大于 9 带来的收益就十分有限了。

另一个真实的例子

生成上千张图片的缩略图 
这是一个 CPU 密集型的任务,并且十分适合进行并行化。

基础单进程版本

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> os <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> PIL <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> multiprocessing <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Pool <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> PIL <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Image<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />SIZE = (<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">75</span>,<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">75</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />SAVE_DIRECTORY = <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> thumbs </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">get_image_paths</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(folder)</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">return</span> (os.path.join(folder, f) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> f <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> os.listdir(folder) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> jpeg </span> <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> f)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">create_thumbnail</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(filename)</span>:</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im = Image.open(filename)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im.thumbnail(SIZE, Image.ANTIALIAS)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    base, fname = os.path.split(filename) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    save_path = os.path.join(base, SAVE_DIRECTORY, fname)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im.save(save_path)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> __name__ == <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> __main__ </span>:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    folder = os.path.abspath(<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> 11_18_2013_R000_IQM_Big_Sur_Mon__e10d1958e7b766c3e840 </span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    os.mkdir(os.path.join(folder, SAVE_DIRECTORY))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    images = get_image_paths(folder)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> image <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> images:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        create_thumbnail(Image)</p>

上边这段代码的主要工作就是将遍历传入的文件夹中的图片文件,一一生成缩略图,并将这些缩略图保存到特定文件夹中。

这我的机器上,用这一程序处理 6000 张图片需要花费 27.9 秒。

如果我们使用 map 函数来代替 for 循环:

<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);color: rgb(248, 248, 242);background: rgb(35, 36, 31);margin-left: 16px;margin-right: 16px;box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> os <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> PIL <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> multiprocessing <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Pool <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">from</span> PIL <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">import</span> Image<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />SIZE = (<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">75</span>,<span class="" style="max-width: 100%;color: rgb(174, 129, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;">75</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />SAVE_DIRECTORY = <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> thumbs </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">get_image_paths</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(folder)</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">return</span> (os.path.join(folder, f) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">for</span> f <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> os.listdir(folder) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> jpeg </span> <span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">in</span> f)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(102, 217, 239);">def</span> <span class="" style="max-width: 100%;color: rgb(166, 226, 46);">create_thumbnail</span><span class="" style="max-width: 100%;color: rgb(248, 248, 242);">(filename)</span>:</span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im = Image.open(filename)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im.thumbnail(SIZE, Image.ANTIALIAS)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    base, fname = os.path.split(filename) <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    save_path = os.path.join(base, SAVE_DIRECTORY, fname)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    im.save(save_path)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span class="" style="max-width: 100%;color: rgb(249, 38, 114);box-sizing: border-box !important;overflow-wrap: break-word !important;">if</span> __name__ == <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> __main__ </span>:<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    folder = os.path.abspath(<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />        <span class="" style="max-width: 100%;color: rgb(230, 219, 116);box-sizing: border-box !important;overflow-wrap: break-word !important;"> 11_18_2013_R000_IQM_Big_Sur_Mon__e10d1958e7b766c3e840 </span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    os.mkdir(os.path.join(folder, SAVE_DIRECTORY))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    images = get_image_paths(folder)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool = Pool()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool.map(creat_thumbnail, images)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool.close()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    pool.join()</p>

5.6 秒!

虽然只改动了几行代码,我们却明显提高了程序的执行速度。在生产环境中,我们可以为 CPU 密集型任务和 IO 密集型任务分别选择多进程和多线程库来进一步提高执行速度——这也是解决死锁问题的良方。此外,由于 map 函数并不支持手动线程管理,反而使得相关的 debug 工作也变得异常简单。

到这里,我们就实现了(基本)通过一行 Python 实现并行化。

《认认真真推荐》


推荐阅读

过节啦收礼物,送给大朋友们精选Python书20本

漫画,杨幂和杨超越到底谁更美

漫画,假装自己是Python高手,学会这十招就可以

本篇文章来源于: 菜鸟学Python

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享