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

来当个伸手党,用 python 写的爬虫,在 win 下内存占用就 70M 多,为什么放到 512m 的 vps 下没多久就被 kill 掉

  •  
  •   sandideas · 2015-07-20 10:33:52 +08:00 · 7711 次点击
    这是一个创建于 3209 天前的主题,其中的信息可能已经有所发展或是发生改变。
    40 条回复    2015-07-21 19:55:19 +08:00
    sandideas
        1
    sandideas  
    OP
       2015-07-20 10:35:51 +08:00
    是linux的vps。。。开了ss之类的,内存占用40%。。感觉剩下的应该够跑吧。
    neoblackcap
        2
    neoblackcap  
       2015-07-20 10:50:55 +08:00   ❤️ 1
    你是不是就直接用shell去启动你的脚本?然后就退出了ssh?
    laiyilong
        3
    laiyilong  
       2015-07-20 10:51:58 +08:00 via Android   ❤️ 1
    没有优化好。。可以考虑多线程,把swap空间调大应该也可以
    icedx
        4
    icedx  
       2015-07-20 10:52:01 +08:00   ❤️ 1
    代码呢
    loading
        5
    loading  
       2015-07-20 10:52:12 +08:00 via Android   ❤️ 1
    cpu超了?
    msg7086
        6
    msg7086  
       2015-07-20 11:10:02 +08:00   ❤️ 1
    虚拟内存设定呢?
    paw
        7
    paw  
       2015-07-20 11:26:02 +08:00   ❤️ 1
    ulimit -a
    看一下,是不是这里限制了,virtual memory;open files ;stack size 等
    以前在用OAH的VPS的时候碰到过,virtual memory有限制,坑爹货。
    sandideas
        8
    sandideas  
    OP
       2015-07-20 12:22:31 +08:00
    @neoblackcap
    对。。。第一次弄这个,难道不是直接启动。。然后扔那么
    kqz901002
        9
    kqz901002  
       2015-07-20 12:23:22 +08:00
    @sandideas nohup $$$&
    sandideas
        10
    sandideas  
    OP
       2015-07-20 12:25:53 +08:00
    @laiyilong 就是多线程。。我查查怎么调swap空间。
    lcqtdwj
        11
    lcqtdwj  
       2015-07-20 12:25:56 +08:00
    win7长时间开着内存也就70M么?
    sandideas
        12
    sandideas  
    OP
       2015-07-20 12:28:14 +08:00
    @icedx 写的很乱。。不敢贴。但是我urlib,file之类的都有close掉。应该没问题吧。
    sandideas
        13
    sandideas  
    OP
       2015-07-20 12:29:19 +08:00
    @loading cpu超了也会么。。好像cpu占用是挺高的。。i7下都占用10+...
    sandideas
        14
    sandideas  
    OP
       2015-07-20 12:30:12 +08:00
    @msg7086
    没去设置。。我觉得应该够吧。。那我去设置下虚拟内存。
    sandideas
        15
    sandideas  
    OP
       2015-07-20 12:30:52 +08:00
    @paw virtual memory是unlimited。。
    sandideas
        16
    sandideas  
    OP
       2015-07-20 12:33:06 +08:00
    @kqz901002 看了下。。这样是为了防止退出ssh被自动关掉么。。但是我连上去后就没关掉。。眼睁睁的看着他显示被kill。。
    sandideas
        17
    sandideas  
    OP
       2015-07-20 12:33:45 +08:00
    @lcqtdwj 对。。。我看了好几次,基本就在70上下。
    kqz901002
        18
    kqz901002  
       2015-07-20 13:07:35 +08:00   ❤️ 1
    @sandideas是的 nohup $command&
    neoblackcap
        19
    neoblackcap  
       2015-07-20 13:12:58 +08:00   ❤️ 1
    @sandideas 贴代码是最快的,你还是贴下代码让大家review一下
    knightlhs
        20
    knightlhs  
       2015-07-20 13:45:15 +08:00   ❤️ 2
    @sandideas 不贴代码很难确认

    从代码的角度思考
    问题可能有:
    1、是否有内存泄露
    2、CPU突发峰值是否超标
    3、内存突发峰值是否超标

    从服务器的角度考虑
    问题可能有:
    1、服务器参数是否虚标 是否超售 特别是 OVZ的类型 内存严重不足
    2、CPU是否被严格限制在一定的指标

    其他暂时还没想到
    msg7086
        21
    msg7086  
       2015-07-20 14:01:55 +08:00   ❤️ 1
    @sandideas 首先你要看的是commit charge,这个关系到进程是否被kill。
    sandideas
        22
    sandideas  
    OP
       2015-07-20 14:08:24 +08:00   ❤️ 1
    @knightlhs
    @msg7086
    @neoblackcap
    @kqz901002
    非常感谢你们回复。。
    找到原因了,因为系统和mysql之类的会占用到百分之40左右的内存。
    而我的代码会在刚开始一段时间从70多到200+。然后才会稳定到70多。。
    原本默认的swap才100m。。我改成1g就够了。这样就不会被系统kill掉了。
    新手。。莫笑。
    sandideas
        23
    sandideas  
    OP
       2015-07-20 14:09:16 +08:00
    @knightlhs cpu超的话会导致进程被kill么。。我看cpu在vps里面一直占到90+。。
    sandideas
        24
    sandideas  
    OP
       2015-07-20 14:12:56 +08:00 via iPhone
    @neoblackcap
    @knightlhs
    不贴代码是因为实在写的渣。。好多异常懒得处理直接忽视了。。
    试运行可以了再优化代码。
    knightlhs
        25
    knightlhs  
       2015-07-20 14:13:25 +08:00
    @sandideas
    这个要看主机商的设定 我这里无法粗浅的下结论
    一般的中小型VPS提供商 长时间占用CPU不但会kill 还会封帐号
    但是也是有区分的 有些专供计算类的云产品 是可以长时间高负荷的 只是价格比较喜人
    具体你要问问你的服务商 具体的要求是什么了
    sandideas
        26
    sandideas  
    OP
       2015-07-20 14:21:21 +08:00 via iPhone
    @knightlhs 了解了。。那我发个tk问一下客服
    zts1993
        27
    zts1993  
       2015-07-20 14:37:19 +08:00   ❤️ 1
    一般主机的后台会有监控 可以查看 CPU内存的使用历史记录
    sandideas
        28
    sandideas  
    OP
       2015-07-20 20:20:42 +08:00
    http://paste.ubuntu.com/11908931/

    @knightlhs
    @neoblackcap
    @icedx
    我还是贴代码吧。。
    虽然可以运行,但是swap速度太慢了。
    开了是个线程内存就会涨到300+
    icedx
        29
    icedx  
       2015-07-21 02:45:58 +08:00   ❤️ 1
    我感觉是你这个bs4 的对象没有被及时回收
    开始代码 soup=BeautifulSoup(page)
    sandideas
        30
    sandideas  
    OP
       2015-07-21 07:59:57 +08:00 via iPhone
    @icedx 好的。。我试试
    sandideas
        31
    sandideas  
    OP
       2015-07-21 09:18:01 +08:00
    @icedx 回收是怎么回收。。我直接del soup不行。。没发现soup有close。。
    icedx
        32
    icedx  
       2015-07-21 10:53:14 +08:00
    @sandideas 应该是函数执行结束后自动回收... 但是多线程下 内存使用瞬间飙升
    sandideas
        33
    sandideas  
    OP
       2015-07-21 11:03:36 +08:00
    @icedx 那就没解决办法了么。。一般爬虫是怎么写的,都会内存占用很多么。
    knightlhs
        34
    knightlhs  
       2015-07-21 11:09:17 +08:00   ❤️ 1
    @sandideas
    我对python不熟悉 我说一下我在go上写爬虫的思路
    首先 你的解析器不需要每个线程上都new一个
    采集线程上只要抓取到页面的信息就OK了 降低负载
    采集回来的信息放进 队列里面 等待解析器统一处理(单例)因为这个玩意大多是单线程的
    解析结果 出来以后 分析是否是列表 还是正文
    如果是列表 那么返回采集队列 继续抓取
    如果是正文 就可以保存了

    一般系统开销都不在 采集这块 我一般可以开到 30-50个并发
    解析器可以多实现几个 分布式 处理 通过 队列随机分配
    这样就可以有效的降低 负载
    供你参考
    sandideas
        35
    sandideas  
    OP
       2015-07-21 11:16:05 +08:00
    @knightlhs 多谢。。我这个最早是单线程的,后来觉得速度慢按照样例改成多线程的。
    的确是没想过解析器不需要多线程。
    待会有空改一下试试。。
    knightlhs
        36
    knightlhs  
       2015-07-21 11:24:48 +08:00   ❤️ 1
    @sandideas
    速度慢的问题 一般都不在采集跟解析 回头你自己写一个分析下
    写入这块你分离出来 用一个单例实现 看看采集完成的时间 (不记写入时间)
    我觉得你速度慢的原因可能在写入 磁盘效率不高 导致队列阻塞了
    如果是这也原因的话 换保存方式 比如 mysql
    sandideas
        37
    sandideas  
    OP
       2015-07-21 12:06:22 +08:00
    @knightlhs 好的。非常感谢
    sandideas
        38
    sandideas  
    OP
       2015-07-21 12:09:52 +08:00
    @knightlhs 那cpu占用很高会有哪些原因。。我在I7下都会达到20%。。。没有多少循环嵌套啊。
    正则匹配的时候会占用很多CPU么。
    knightlhs
        39
    knightlhs  
       2015-07-21 15:32:44 +08:00
    @sandideas 正则的效率确实不高 你需要使用一些基于DOM的解析引擎
    icedx
        40
    icedx  
       2015-07-21 19:55:19 +08:00
    @sandideas 你要吧bs4 处理的过程放在独立的函数里 不要返回bs4 对象
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2098 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:30 · PVG 19:30 · LAX 04:30 · JFK 07:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.