首页 > 解决方案 > 调用 requests.get(...) 随机杀死线程。为什么?

问题描述

我有一个Python 2.7 脚本,我在其中requests.get(...)以顺序方式对许多服务器进行 GET 调用。但是,有时,我正在拨打电话的线程在拨打电话时会被杀死requests.get(...),我不知道为什么。我知道这一点是因为我在拨打电话之前和之后打印了日志,并且最后一个日志条目始终是拨打电话之前的那个。使用以下命令进行调用:

r = requests.get("http://" + node.hostname + ":2082/api/config", timeout=1)

这是进行调用的方法的基本框架:

def refresh_current_configs(number):
    while True:
        try:
            for node in nodes:
                try:
                    r = requests.get("http://" + node.hostname + ":2082/api/config", timeout=1)
                except (ConnectionError, requests.exceptions.Timeout) as e:
                    unreachable_nodes.add(node.hostname)
                    continue

                # Do processing

        except Exception,e:
            logger.error(e + " -- " + str(number))
        time.sleep(1000)

这就是我创建一个在refresh_current_configs主线程中运行方法的线程的方式:

consumer = threading.Thread(target=refresh_current_configs,args=(number,),name='config_refresher')
consumer.start()

任何想法为什么调用requests.get(...)会随机杀死进行调用的线程?

标签: pythonmultithreadingpython-2.7python-requestspython-multithreading

解决方案


推荐阅读