擅长:python、mysql、java
<p>FWIW,<code>multiprocessing</code>模块使用<code>Pool</code>类有一个很好的接口。如果您想坚持使用线程而不是进程,那么可以使用<code>multiprocessing.pool.ThreadPool</code>类作为替换。</p>
<pre><code>def foo(bar, baz):
print 'hello {0}'.format(bar)
return 'foo' + baz
from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=1)
async_result = pool.apply_async(foo, ('world', 'foo')) # tuple of args for foo
# do some other stuff in the main process
return_val = async_result.get() # get the return value from your function.
</code></pre>