推荐学习书目
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
Phishion
V2EX  ›  Python

如何优化多线程/进程条件下,关闭连接池的用时?

  •  
  •   Phishion · Dec 27, 2021 · 2587 views
    This topic created in 1623 days ago, the information mentioned may be changed or developed.

    伪代码如下,我发现整个函数的执行时间倒不是很长,但是关闭连接池,竟然要消耗 1 秒,看起来不多,几千条数据累加起来也挺不得了的,那么有什么办法优化这一部分的代码,让它的关闭耗时没那么高?

    from multiprocessing.dummy import Pool as ThreadPool
    
    def my_function(page):
        print(page)
    
    pages_offset = [0, 100, 200, 300]
    pool = ThreadPool(5)
    pool.map(my_function, pages_offset)
    pool.close()
    pool.join()
    
    Supplement 1  ·  Dec 28, 2021
    问题已经找到,是关闭打开连接池太频繁,虽然我的设计是一个连接池周期处理 7 ~ 8 个任务,但是这些任务处理的都非常快,所以连接池的开关瓶颈就出现了
    6 replies    2021-12-29 10:41:24 +08:00
    hahaba
        1
    hahaba  
       Dec 27, 2021
    建议把业务逻辑说出来,最优解不一定要用上诉伪代码实现
    LeeReamond
        2
    LeeReamond  
       Dec 27, 2021
    为什么从多进程库引入的会叫“线程池”,这是进程池啊
    shyling
        3
    shyling  
       Dec 27, 2021
    几千条数据要关系几千次 [连接池] ?
    shyling
        4
    shyling  
       Dec 27, 2021
    关系->关闭
    Phishion
        5
    Phishion  
    OP
       Dec 28, 2021
    @shyling 我也找到原因了,我关闭连接池太频繁了,现在已经解决了,我之前都是多线程跑网络数据瓶颈不在这上面,现在性能终于上来了,谢谢
    ruanimal
        6
    ruanimal  
       Dec 29, 2021
    @LeeReamond multiprocessing.dummy 没用过?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4020 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 94ms · UTC 05:11 · PVG 13:11 · LAX 22:11 · JFK 01:11
    ♥ Do have faith in what you're doing.