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

Python 的第三方库到底行不行啊?

  •  
  •   gouchaoer · 2018-01-19 15:07:05 +08:00 · 3678 次点击
    这是一个创建于 2282 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这两天写个爬虫调度的东西,用了 redis 库:

    RUN pip2.7 install redis -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com

    然后我的 py 脚本在一个 while 循环里面跑,跑着跑着发现就挂住了,用 strace 和 lsof 一看发现:

    python2.7 292 root    0u   CHR   136,1      0t0                4 /dev/pts/1
    python2.7 292 root    1u   CHR   136,1      0t0                4 /dev/pts/1
    python2.7 292 root    2u   CHR   136,1      0t0                4 /dev/pts/1
    python2.7 292 root    3u  sock     0,7      0t0          3629287 can't identify protocol
    python2.7 292 root    4u  IPv4 3628745      0t0              TCP d602b1d9c3a8:34926->---------:6379 (ESTABLISHED)
    python2.7 292 root   12r   CHR     1,9      0t0          3622123 /dev/urandom
    [root@d602b1d9c3a8 /]# strace -p 292
    Process 292 attached
    recvfrom(4,
    

    在接收某个 redis 的 tcp 调用地方卡住了,于是跑回去看 while 循环里面对 redis 的操作不是 set 就是 get,唯一一个值得怀疑的地方是:

                        while True:
                            # blpop
                            now = time.time()
                            diff = now - self.pre
                            bpopTime = self.loopTime - diff
                            bpopTime = int(bpopTime)
                            if bpopTime <= 0:
                                break;
                            if bpopTime > self.loopTime:
                                bpopTime = self.loopTime
                            req_json_tuple = self.redis.blpop("ktgg_node_id_list:" + self.log.node_id, bpopTime)
    

    没问题啊,我这个超时时间只能是 1、2、3、4、5 这几个正整数,神了

    13 条回复    2018-01-30 18:16:28 +08:00
    gouchaoer
        1
    gouchaoer  
    OP
       2018-01-19 16:12:54 +08:00
    怎么这个策略没用啊,说好的想问一个问题先 diss 一下就能有解决方案上门的呢
    ping2
        2
    ping2  
       2018-01-19 18:34:20 +08:00
    单个循环能卡住的话代码问题吧? QPS 上不去才是性能问题。当然,官方文档里说了,想性能高的话用 hiredis。
    wwqgtxx
        3
    wwqgtxx  
       2018-01-19 19:18:52 +08:00
    你 debug 好歹也要用 pdb 看看到底卡在哪里了吧,另外看到你用了 time.time()那么请检查一下你的系统时间是否在运行时被修改过
    gouchaoer
        4
    gouchaoer  
    OP
       2018-01-19 22:21:59 +08:00 via Android
    nyanyh
        5
    nyanyh  
       2018-01-19 22:33:40 +08:00
    因为你这是地图炮啊
    得说 redis 不行,这样会有人上门
    huntzhan
        6
    huntzhan  
       2018-01-20 02:28:54 +08:00
    Block,走好不送
    lolizeppelin
        7
    lolizeppelin  
       2018-01-20 10:16:15 +08:00 via Android
    一出问题就怀疑三方模块 你还写个屁代码

    再说 redis 模块就那么点代码 redis 基本操作也就那么多 随便花点时间就看完了
    gouchaoer
        8
    gouchaoer  
    OP
       2018-01-20 11:28:56 +08:00
    用那个 issue 里面的 hack 办法跑了一晚上效果如下:
    ```
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ```
    只是我给所有 redis 操作设置 hack 了一个 socket 超时,这样 brpop 超时了就硬返回了。。。。大家注意到这个 redis 的 tcp 连接我是没有断的,也就是说的确是这个库底层的问题
    gouchaoer
        9
    gouchaoer  
    OP
       2018-01-20 11:32:05 +08:00
    主要是 python 的库给了我一些麻烦,比如 mysql 官方的那个客户端,连着连着就断了,然后就只能这样:
    ```
    try:
    cursor = self.db.cursor()
    cursor.execute(sql)
    cursor.close()
    except Exception, e:
    #连接可能会断,这里从连一下
    self.db = connector.connect(**getMysqlConfig())
    cursor = self.db.cursor()
    cursor.execute(sql)
    cursor.close()
    ```
    比较 dirty
    guyskk0x0
        10
    guyskk0x0  
       2018-01-20 12:41:54 +08:00 via Android
    IO 会阻塞超时很正常,底层库不可能自动重试,只有应用层才知道什么时候能重试
    lolizeppelin
        11
    lolizeppelin  
       2018-01-20 19:35:32 +08:00 via Android
    什么连接都连着连着断了 和 Python 库毛关系
    做了心跳没 写了池化了没
    随便开撸的代码就做好出错的准备 烂代码怪库头上

    PHP 写多了吧?
    gouchaoer
        12
    gouchaoer  
    OP
       2018-01-20 19:39:27 +08:00 via Android
    @lolizeppelin 写 py 不太多,php 写的多一点
    gouchaoer
        13
    gouchaoer  
    OP
       2018-01-30 18:16:28 +08:00 via Android
    大家好我又来了,结果 php 的 pdo 库也是连接 mysql 久了会弹异常,不过 predis 的 blpop 倒没啥问题。。。。这说了了 cli 进程长时间运行需要注意保存连接
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5106 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:16 · PVG 16:16 · LAX 01:16 · JFK 04:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.