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
Phishion
V2EX  ›  Python

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

  •  
  •   Phishion · 2021-12-27 17:16:53 +08:00 · 2023 次点击
    这是一个创建于 848 天前的主题,其中的信息可能已经有所发展或是发生改变。

    伪代码如下,我发现整个函数的执行时间倒不是很长,但是关闭连接池,竟然要消耗 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()
    
    第 1 条附言  ·  2021-12-28 02:22:20 +08:00
    问题已经找到,是关闭打开连接池太频繁,虽然我的设计是一个连接池周期处理 7 ~ 8 个任务,但是这些任务处理的都非常快,所以连接池的开关瓶颈就出现了
    6 条回复    2021-12-29 10:41:24 +08:00
    hahaba
        1
    hahaba  
       2021-12-27 18:39:27 +08:00
    建议把业务逻辑说出来,最优解不一定要用上诉伪代码实现
    LeeReamond
        2
    LeeReamond  
       2021-12-27 19:43:46 +08:00
    为什么从多进程库引入的会叫“线程池”,这是进程池啊
    shyling
        3
    shyling  
       2021-12-27 19:53:43 +08:00
    几千条数据要关系几千次 [连接池] ?
    shyling
        4
    shyling  
       2021-12-27 19:53:51 +08:00
    关系->关闭
    Phishion
        5
    Phishion  
    OP
       2021-12-28 02:20:08 +08:00
    @shyling 我也找到原因了,我关闭连接池太频繁了,现在已经解决了,我之前都是多线程跑网络数据瓶颈不在这上面,现在性能终于上来了,谢谢
    ruanimal
        6
    ruanimal  
       2021-12-29 10:41:24 +08:00
    @LeeReamond multiprocessing.dummy 没用过?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2894 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:23 · PVG 23:23 · LAX 08:23 · JFK 11:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.