本文介绍了在请求库中,如何避免"HttpConnectionPool已满,丢弃连接"?警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有会话的python请求库:

I'm using python requests library with sessions:

def _get_session(self):
    if not self.session:
        self.session = requests.Session()
    return self.session

有时我的日志中会出现此警告:

And sometimes I'm getting this warning in my logs:

[2014/May/12 14:40:04 WARNING ] HttpConnectionPool is full, discarding connection: www.ebi.ac.uk

我的问题是:为什么这是警告而不是例外?

My question is: why this is warning and not an exception?

这是对此负责的代码(来自 http://pydoc.net /Python/requests/0.8.5/requests.packages.urllib3.connectionpool/):

This is the code responsible for this (from http://pydoc.net/Python/requests/0.8.5/requests.packages.urllib3.connectionpool/):

def _put_conn(self, conn):
    try:
        self.pool.put(conn, block=False)
    except Full:
        # This should never happen if self.block == True
        log.warning("HttpConnectionPool is full, discarding connection: %s"
                    % self.host)

为什么在此捕获此异常?如果重新引发了该异常,则可以通过创建新的会话并删除旧的会话来处理我的代码中的异常.

Why this exception is catched here? If it was reraised, I could handle this exception in my code, by creating new session and deleting the old one.

如果仅是警告,是否表示它不会以任何方式影响我的结果?我可以忽略它吗?如果没有,我该如何处理这种情况?

If it's only a warning, does it mean it doesn't affect my results in any way? Can I ignore it? If not, how can I handle this situation?

推荐答案

来自 http://docs.python-requests.org/en/latest/api/

 class requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=0, pool_block=False)

用于urllib3的内置HTTP适配器.

The built-in HTTP Adapter for urllib3.

通过实现传输适配器为请求会话提供联系HTTP和HTTPS url的通用接口 界面.此类通常由Session类创建 在幕后.

Provides a general-case interface for Requests sessions to contact HTTP and HTTPS urls by implementing the Transport Adapter interface. This class will usually be created by the Session class under the covers.

参数:

  • pool_connections –要缓存的urllib3连接池的数量.
  • pool_maxsize –要保存在池中的最大连接数.
  • max_retries(int)–每个连接应尝试的最大重试次数.请注意,这仅适用于失败的连接和超时,不适用于服务器返回响应的请求.
  • pool_block –连接池是否应阻止连接.
  • pool_connections – The number of urllib3 connection pools to cache.
  • pool_maxsize – The maximum number of connections to save in the pool.
  • max_retries (int) – The maximum number of retries each connection should attempt. Note, this applies only to failed connections and timeouts, never to requests where the server returns a response.
  • pool_block – Whether the connection pool should block for connections.

还有下面的例子

import requests
s = requests.Session()
a = requests.adapters.HTTPAdapter(max_retries=3)
s.mount('http://', a)

尝试一下

a = requests.adapters.HTTPAdapter(pool_connections = N, pool_maxsize = M)

N和M都适合您的程序.

Where N and M are suitable for your program.

这篇关于在请求库中,如何避免"HttpConnectionPool已满,丢弃连接"?警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:35