V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
suannva
V2EX  ›  Python

Python threading 如何控制线程数?

  •  
  •   suannva · 2018-03-11 15:36:37 +08:00 · 2799 次点击
    这是一个创建于 2210 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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() 写一个东西 目前是这么用的 怎么控制线程数量

    6 条回复    2018-03-13 17:52:09 +08:00
    pusidun
        1
    pusidun  
       2018-03-11 16:07:39 +08:00
    线程池
    ```
    pip install threadpool
    ```
    BBCCBB
        2
    BBCCBB  
       2018-03-11 19:27:26 +08:00
    ```python
    from multiprocessing.dummy import Pool as ThreadPool
    ```
    matsuz
        3
    matsuz  
       2018-03-11 21:36:43 +08:00 via iPhone   ❤️ 2
    concurrent.future.ThreadPoolExecutor
    jackqian
        4
    jackqian  
       2018-03-11 22:16:07 +08:00 via iPhone
    应该有类似 java 的线程池
    hanbaobao2005
        5
    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
        6
    Mojy  
       2018-03-13 17:52:09 +08:00
    @hanbaobao2005 这个应该是进程池吧?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2788 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:45 · PVG 22:45 · LAX 07:45 · JFK 10:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.