[求助] Python 新手遇到诡异的内存泄漏问题,大神们来帮忙 - V2EX
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
TeslaM3
V2EX    Python

[求助] Python 新手遇到诡异的内存泄漏问题,大神们来帮忙

  •  
  •   TeslaM3 2024-07-18 11:23:09 +08:00 1736 次点击
    这是一个创建于 516 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位大佬们,我最近在学习 Python 爬虫,遇到个奇怪的问题。我在处理大量数据时,程序运行一段时间后会突然卡死,排查发现可能是内存泄漏。代码里用到 requests 和 BeautifulSoup ,但怎么检查也找不出问题所在。有没有熟悉这方面的老司机能指点迷津?附上部分关键代码:

    def fetch_data(url): respOnse= requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') data = soup.find_all('div', class_='data-element') return data # 无限循环爬取 while True: data_list = fetch_data('http://xxx.com') process_data(data_list) 

    如果是我理解错了,请纠正。感激不尽!

    TeslaM3
        1
    TeslaM3  
    OP
       2024-07-18 14:28:56 +08:00
    怎么无人!
    ZhaiSoul
        2
    ZhaiSoul  
       2024-07-18 15:12:50 +08:00
    Python 再怎么慢,你这个 while 循环一直爬取……也过分了点吧,你没有进行周期等待的话,每秒可能执行上万次,GC 都还没来得及你就已经炸了
    ZhaiSoul
        3
    ZhaiSoul  
       2024-07-18 15:16:17 +08:00
    @ZhaiSoul 哦,查了一下,requests 不是异步的,那我收回前面的话
    不过我个人还是建议用异步的请求库会好一些
    TeslaM3
        4
    TeslaM3  
    OP
       2024-07-18 15:34:33 +08:00
    ClericPy
        5
    ClericPy  
       2024-07-18 19:01:29 +08:00   1
    突然卡死你是怎么定位出内存泄漏这个类别的。。。

    Requests 这个库当年确实会有各种问题,很早年有个 session cache 相关的死锁还是什么会卡住一直不继续,后来还遇到过 session 、Response 没正确关闭导致的类似泄漏的情况,所以长时间运行的基本都不用默认 requests 发请求,而是一直 with session 和 with Response


    信息太少,只能提供思路:
    1. Python 有很多进程运行时工具可以看出你 hang 在哪一行的,先定位下是哪一行再直接撸 Requests 源码找细节。反正就是先看是哪一行停住了
    2. 升级 Requests 库,或者换 httpx ,反正 api 都一样的,看看能复现没有
    3. 你这个 Requests 但凡加个 Timeout ,也能让别人排除一个问题,有时候 TCP 操作不当或者 Server 写的不行,确实会挂住连接
    TeslaM3
        6
    TeslaM3  
    OP
       2024-07-18 19:38:30 +08:00
    @ClericPy 谢谢,刚学习 py
    lLuO0WljRTqf
        7
    lLuO0WljRTqf  
       2024-07-18 21:53:13 +08:00
    requests 连接的时候我记得有个 timeout 的参数。
    另外,爬取时请加延时。你访问那么快,如何保证对方网站不禁你 ip 一段时间呢?
    wang93wei
        8
    wang93wei  
       2024-07-23 15:32:22 +08:00
    我在处理大量数据时,程序运行一段时间后会突然卡死,排查发现可能是内存泄漏。

    你的 process_data 呢?为啥会觉得 fetch_data 会出问题?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5168 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 47ms UTC 07:38 PVG 15:38 LAX 23:38 JFK 02:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86