<p>你的代码似乎对我有用:</p>
<pre><code>from multiprocessing import Pool
import time
def new_awesome_function(a,b):
print(a,b, 'start')
time.sleep(1)
print(a,b, 'end')
if __name__ == '__main__':
data = [1,2,3,4,5]
pool = Pool(processes=4)
for i, x in enumerate(data):
pool.apply_async(new_awesome_function, (i, x))
pool.close()
pool.join()
</code></pre>
<p>给了我:</p>
<pre><code>0 1 start
1 2 start
2 3 start
3 4 start
1 2 end
0 1 end
4 5 start
2 3 end
3 4 end
4 5 end
</code></pre>
<p>是什么让你觉得它不起作用?</p>
<hr/>
<p><strong>编辑:</strong>尝试运行此命令并查看输出:</p>
<pre><code>from multiprocessing import Pool
import time
def new_awesome_function(a,b):
print(a,b, 'start')
time.sleep(1)
print(a,b, 'end')
return a + b
if __name__ == '__main__':
data = [1,2,3,4,5]
pool = Pool(processes=4)
results = []
for i, x in enumerate(data):
r = pool.apply_async(new_awesome_function, (i, x))
results.append((i,r))
pool.close()
already = []
while len(already) < len(data):
for i,r in results:
if r.ready() and i not in already:
already.append(i)
print(i, 'is ready!')
pool.join()
</code></pre>
<p>我的是:</p>
<pre><code>0 1 start
1 2 start
2 3 start
3 4 start
0 1 end
4 5 start
1 2 end
2 3 end
0 is ready!
3 4 end
1 is ready!
2 is ready!
3 is ready!
4 5 end
4 is ready!
</code></pre>