在multiprocessing模块中ThreadPool和Pool之间的区别是什么。当我尝试我的代码时,这是我看到的主要区别:from multiprocessing import Pool
import os, time
print("hi outside of main()")
def hello(x):
print("inside hello()")
print("Proccess id: ", os.getpid())
time.sleep(3)
return x*x
if __name__ == "__main__":
p = Pool(5)
pool_output = p.map(hello, range(3))
print(pool_output)
我看到以下输出:hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
inside hello()
Proccess id: 13268
inside hello()
Proccess id: 11104
inside hello()
Proccess id: 13064
[0, 1, 4]
使用“ThreadPool”:from multiprocessing.pool import ThreadPool
import os, time
print("hi outside of main()")
def hello(x):
print("inside hello()")
print("Proccess id: ", os.getpid())
time.sleep(3)
return x*x
if __name__ == "__main__":
p = ThreadPool(5)
pool_output = p.map(hello, range(3))
print(pool_output)
我看到以下输出:hi outside of main()
inside hello()
inside hello()
Proccess id: 15204
Proccess id: 15204
inside hello()
Proccess id: 15204
[0, 1, 4]
我的问题是:为什么每次在Pool中运行“outside\uu main”?
multiprocessing.pool.ThreadPool不产生新进程?它只是创造了新的线索?
如果是这样的话,使用multiprocessing.pool.ThreadPool与使用threading模块有什么区别?
我在任何地方都看不到任何关于ThreadPool的官方文档,有人能帮我找到它吗?