Python threading 如何控制线程数?

2018-03-11 15:36:37 +08:00
 suannva

if name == "main": file = open("mssql.ip",'r') for ip in file.readlines(): ip = ip.strip('\n') t1 = Thread(target=content,args=(ip,)) t1.start() 写一个东西 目前是这么用的 怎么控制线程数量

2826 次点击
所在节点    Python
6 条回复
pusidun
2018-03-11 16:07:39 +08:00
线程池
```
pip install threadpool
```
BBCCBB
2018-03-11 19:27:26 +08:00
```python
from multiprocessing.dummy import Pool as ThreadPool
```
matsuz
2018-03-11 21:36:43 +08:00
concurrent.future.ThreadPoolExecutor
jackqian
2018-03-11 22:16:07 +08:00
应该有类似 java 的线程池
hanbaobao2005
2018-03-12 09:47:11 +08:00
https://segmentfault.com/a/1190000000414339

from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool()
pool = ThreadPool(4) # Sets the pool size to 4
results = pool.map(func, paras)
pool.close()
pool.join()
Mojy
2018-03-13 17:52:09 +08:00
@hanbaobao2005 这个应该是进程池吧?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/436980

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX