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

2021 年了, requests 内存泄露的问题解决了吗?如果没解决,怎么解决?

  •  
  •   v2exblog · 2021-06-15 15:46:12 +08:00 · 4500 次点击
    这是一个创建于 1017 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这是在网上看到的一篇博客,
    https://morningchen.com/2019/12/12/celery-memory-leak/


    用 docker 在最新的 python 版本上实验,还是有泄露的情况。有朋友知道怎么办吗?
    第 1 条附言  ·  2021-06-15 16:42:48 +08:00

    '''

    Line # Mem usage Increment Occurences Line Contents

     7  42.5820312500 MiB  42.5820312500 MiB           1   @profile(precision=10)
     8                                         def foo():
     9  42.5820312500 MiB   0.0000000000 MiB           1       gc.collect()
    10  43.0156250000 MiB   0.4335937500 MiB           1       print(len(gc.get_objects()))
    11  43.0156250000 MiB   0.0000000000 MiB           1       session = requests.Session()
    12                                         
    13  45.9453125000 MiB   2.9296875000 MiB           1       r = session.get('https://v2ex.com/4')
    14  45.9453125000 MiB   0.0000000000 MiB           1       r.close()
    15                                         
    16  46.2070312500 MiB   0.2617187500 MiB           1       r = session.get('https://v2ex.com')
    17  46.2070312500 MiB   0.0000000000 MiB           1       r.close()
    18                                         
    19  46.2343750000 MiB   0.0273437500 MiB           1       r = session.get('https://v2ex.com/1')
    20  46.2343750000 MiB   0.0000000000 MiB           1       r.close()
    21                                         
    22  46.2421875000 MiB   0.0078125000 MiB           1       r = session.get('https://v2ex.com/2')
    23  46.2421875000 MiB   0.0000000000 MiB           1       r.close()
    24                                         
    25  46.2500000000 MiB   0.0078125000 MiB           1       session.close()
    26  46.2500000000 MiB   0.0000000000 MiB           1       del session
    27  46.2500000000 MiB   0.0000000000 MiB           1       del r
    28                                         
    29  46.2500000000 MiB   0.0000000000 MiB           1       gc.collect()
    30  46.5390625000 MiB   0.2890625000 MiB           1       print(len(gc.get_objects()))
    

    '''

    7 条回复    2021-06-15 21:36:18 +08:00
    est
        1
    est  
       2021-06-15 16:24:42 +08:00
    https://github.com/psf/requests/issues/4601

    Call Session.close() and Response.close() can avoid the memory leak.
    est
        2
    est  
       2021-06-15 16:25:42 +08:00
    LZ 可以试下上面给出的方法。如果成功了。那其实不算泄漏。requests 头铁忠实实现了 http/1.1 的 keep-connection 连接复用而已。
    v2exblog
        3
    v2exblog  
    OP
       2021-06-15 16:44:30 +08:00
    @est 刚才贴了一段简单的代码,环境是 python3.7,似乎 Call Session.close() and Response.close() 内存没有明显减少
    ospider
        4
    ospider  
       2021-06-15 17:23:56 +08:00
    解决方法就是用 httpx,放弃 requests
    Trim21
        5
    Trim21  
       2021-06-15 17:33:39 +08:00 via Android
    httpx 底层跟 requests 不一样,不知道有没有泄漏的问题。
    Huelse
        6
    Huelse  
       2021-06-15 20:57:52 +08:00
    LeeReamond
        7
    LeeReamond  
       2021-06-15 21:36:18 +08:00
    @v2exblog 内存释放也不会将堆内存还给系统,我觉得没有减少不代表泄露
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3210 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:39 · PVG 19:39 · LAX 04:39 · JFK 07:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.