擅长:python、mysql、java
<blockquote>
<p><strong>Question</strong>: it seems didn't work (as I saw in Task Manager that only 1 process was running)
and result is never delivered.</p>
</blockquote>
<p>您只看到1个进程</strong>作为<code>Pool</code>计算使用的进程数如下:<br/>
你给出了<code>range(10)</code>=任务索引0..9,因此<code>Pool</code>计算<code>(10 / 4) * 4 = 8+1 = 9</code>。<br/>
首先启动<strong>后</strong><code>process</code>就没有任务了。<br/>
使用<code>range(32)</code>,您将看到<strong>4</strong><code>process</code>正在运行。</p>
<p>您返回的是<code>return t</code>,而不是<code>rs = pool.map(...</code>的结果。</p>
<hr/>
<p>例如,这将起作用</p>
<pre><code>def cal(self, nb):
import os
print('pid:{} cal({})'.format(os.getpid(), nb))
return nb * self.vl
def run(self,df):
with mp.Pool(processes=4) as pool:
return pool.map(self.cal, df)
if __name__ == '__main__':
a = A(2)
result = a.run(list(range(32)))
print(result)
</code></pre>
<p><strong><em>使用Python测试:3.4.2</em></strong></p>