【Python】Python多进程库multiprocessing中进程池Pool的返回值
<h1 style="padding:0px;font-family:'-apple-system','SF UI Text',Arial,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','WenQuanYi Micro Hei',sans-serif,SimHei,SimSun;background-color:rgb(255,255,255);">问题起因<p style="font-family:'-apple-system',255);">最近要将一个文本分割成好几个topic,每个topic设计一个regressor,各regressor是相互独立的,最后汇总所有topic的regressor得到总得预测结果。没错!类似bagging ensemble!只是我没有抽样。文本不大,大概3000行,topic个数为8,于是我写了一个串行的程序,一个topic算完之后再算另一个topic。可是我在每个topic中用了<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">GridSearchCV来调参,又要选特征又要调整regressor的参数,导致参数组合一共有1782种。我真是低估了调参的时间,程序跑了一天一夜最后因为忘记import一个库导致最终的预测精度没有算出来。后来想到,既然每个topic的预测都是独立的,那是不是可以并行呢? <h1 style="padding:0px;font-family:'-apple-system',255);">Python中的多线程与多进程<p style="font-family:'-apple-system',255);">但是听闻Python的多线程实际上并不能真正利用多核,所以如果使用多线程实际上还是在一个核上做并发处理。不过,如果使用多进程就可以真正利用多核,因为各进程之间是相互独立的,不共享资源,可以在不同的核上执行不同的进程,达到并行的效果。同时在我的问题中,各topic相互独立,不涉及进程间的通信,只需最后汇总结果,因此使用多进程是个不错的选择。<h1 style="padding:0px;font-family:'-apple-system',255);">multiprocessing<h2 style="padding:0px;font-family:'-apple-system',255);">一个子进程<p style="font-family:'-apple-system',255);">multiprocessing模块提供process类实现新建进程。下述代码是新建一个子进程。<pre class="prettyprint" style="font-size:14px;line-height:22px;"><code class="language-python hljs has-numbering"><span class="hljs-keyword">from multiprocessing <span class="hljs-keyword">import Process<span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">f<span class="hljs-params">(name): <span class="hljs-keyword">if name == <span class="hljs-string">'main': <span class="hljs-keyword">import time <span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">func<span class="hljs-params">(msg): <span class="hljs-keyword">if name == <span class="hljs-string">"main": PoolWorker<span class="hljs-subst">-<span class="hljs-number">34<span class="hljs-attribute">-hello <span class="hljs-number">1 PoolWorker<span class="hljs-subst">-<span class="hljs-number">33<span class="hljs-attribute">-hello <span class="hljs-number">0 PoolWorker<span class="hljs-subst">-<span class="hljs-number">35<span class="hljs-attribute">-hello <span class="hljs-number">2 PoolWorker<span class="hljs-subst">-<span class="hljs-number">36<span class="hljs-attribute">-hello <span class="hljs-number">3 PoolWorker<span class="hljs-subst">-<span class="hljs-number">34<span class="hljs-attribute">-hello <span class="hljs-number">7 PoolWorker<span class="hljs-subst">-<span class="hljs-number">33<span class="hljs-attribute">-hello <span class="hljs-number">4 PoolWorker<span class="hljs-subst">-<span class="hljs-number">35<span class="hljs-attribute">-hello <span class="hljs-number">5 PoolWorker<span class="hljs-subst">-<span class="hljs-number">36<span class="hljs-attribute">-hello <span class="hljs-number">6 PoolWorker<span class="hljs-subst">-<span class="hljs-number">33<span class="hljs-attribute">-hello <span class="hljs-number">8 PoolWorker<span class="hljs-subst">-<span class="hljs-number">36<span class="hljs-attribute">-hello <span class="hljs-number">9<ul class="pre-numbering"><li style="color:rgb(153,153);">11<p style="font-family:'-apple-system',255);">上述代码中的<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">pool.apply_async()是<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply()函数的变体,<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply_async()是<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply()的并行版本,<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply()是<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply_async()的阻塞版本,使用<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply()主进程会被阻塞直到函数执行结束,所以说是阻塞版本。<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply()既是<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">Pool的方法,也是Python内置的函数,两者等价。可以看到输出结果并不是按照代码for循环中的顺序输出的。<h1 style="padding:0px;font-family:'-apple-system',255);">多个子进程并返回值<p style="font-family:'-apple-system',255);"><code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">apply_async()本身就可以返回被进程调用的函数的返回值。上一个创建多个子进程的代码中,如果在函数<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">func中返回一个值,那么<code style="font-size:14px;line-height:22px;padding:4px 2px 0px;">pool.apply_async(func,))的结果就是返回pool中所有进程的<span style="font-weight:700;">值的对象(注意是对象,不是值本身)。<pre class="prettyprint" style="font-size:14px;line-height:22px;"><code class="language-python hljs has-numbering"><span class="hljs-keyword">import multiprocessing <span class="hljs-keyword">import time <span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">func<span class="hljs-params">(msg): <span class="hljs-keyword">if name == <span class="hljs-string">"main":
PoolWorker<span class="hljs-subst">-<span class="hljs-number">37<span class="hljs-attribute">-hello <span class="hljs-number">0 转自:https://blog.csdn.net/jinping_shi/article/details/52433867 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |