一次python进程卡死情况排查(下)

1,651 阅读1分钟

上文排查到python进程阻塞在一个网络请求,但是为什么会阻塞到还没确定。
查了下资料,可以使用Python 的性能分析工具大杀器: py-spy

pip install py-spy
#--locals会显示局部变量
py-spy dump --pid 1 --locals
#查看调用栈,发现在发送request请求时没有添加超时参数。
request (requests/sessions.py:488)
        Arguments::
            self: <Session at 0x7f5e2ad39ba8>
            method: "get"
            url: "https://www.xxx.com/"
            params: None
            data: None
            headers: {"Referer": "https://www.xxx.com/"}
            cookies: None
            files: None
            auth: None
            timeout: None  #重要此处timeout没有设置
            allow_redirects: True
            proxies: {}
            hooks: None
            stream: None
            verify: None
            cert: None
            json: None
        Locals::
            req: <Request at 0x7f5de29e56d8>
            prep: <PreparedRequest at 0x7f5de29e55c0>
            settings: {"verify": True, "proxies": {}, "stream": False, "cert": None}
            send_kwargs: {"timeout": None, "allow_redirects": True, "verify": True, "proxies": {}, "stream": False, "cert": None}

Requests文档:

致此,问题排查结束,对Requests代码添加超时参数。

参考链接: www.cnblogs.com/ljz-2014/p/…
requests.readthedocs.io/zh_CN/lates…