python threadpool_Python多处理模块中ThreadPool与Pool之间的区别...

本文探讨了Python多处理模块中ThreadPool和Pool的主要区别。通过示例代码展示了Pool在运行时会创建多个进程,每个进程都有独立的内存空间,而ThreadPool则在同一进程中创建多个线程。讨论了为何在Pool中`__name__ == "__main__"`会多次打印,以及ThreadPool与线程模块的使用差异,并询问了ThreadPool的官方文档位置。
摘要由CSDN通过智能技术生成

什么是多处理模块中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 __main __()”?

> multiprocessing.pool.ThreadPool不会产生新进程?它只是创建新线程?

>如果是这样,使用multiprocessing.pool.ThreadPool而不仅仅是线程模块之间的区别是什么?

我没有在任何地方看到ThreadPool的官方文档,有人可以帮我找到它吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值