擅长:python、mysql、java
<p>您正在寻找<code>map_async</code><a href="https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.AsyncResult" rel="nofollow noreferrer">https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.AsyncResult</a>,而不是<code>map</code>。这是你的例子。这可以应用于任意数量的函数调用。所有这些都将异步执行。你知道吗</p>
<pre><code>def do_smth_parallel(query):
pool = Pool(processes=2)
result = []
arg = [ query ]
future_1 = pool.async_map(api_request_1, arg)
future_2 = pool.async_map(api_request_2, arg)
result_1 = future_1.get()
results_2 = future_2.get()
pool.close()
pool.join()
return result_1 + result_2
</code></pre>