上面的代码我们也可以改写为进程池形式,api和线程池如出一辙,我就不罗嗦了。
# example2.pyfrom concurrent.futures import ProcessPoolExecutorimport timedef return_future_result(message): time.sleep(2) return messagepool = ProcessPoolExecutor(max_workers=2)future1 = pool.submit(return_future_result, ("hello"))future2 = pool.submit(return_future_result, ("world"))print(future1.done())time.sleep(3)print(future2.done())print(future1.result())print(future2.result())
下面是运行结果
ziwenxie :: ~ » python example2.pyFalseTruehelloworldziwenxie :: ~ » ps -eLf | grep pythonziwenxie 8560 7557