【问题标题】:Does requests.session expires if remained idle?如果保持空闲,requests.session 会过期吗?
【发布时间】:2021-07-16 06:20:05
【问题描述】:

我在重试请求函数中使用了 request.session()。我想知道会话持续多长时间。如果session 对象保持空闲一段时间,它会在一段时间后过期吗?

def create_requests_retry_session(
        retries=app.config["MAX_RETRY_FOR_SESSION"],
        backoff_factor=app.config["BACKOFF_FACTOR"]):
    """
    implement failure mechanism while calling microservices to avoid system alert
    using parameters like a time limit to get a response. This function will attempt 3 times
    per session
    retries: Defaults to MAX_RETRY_FOR_SESSION
    backoff_factor: how long the processes will sleep between failed requests.
    Defaults to BACKOFF_FACTOR {backoff factor} * (2 ** ({number of total
    retries} - 1)). If the backoff_factor is 0.1, then sleep() will sleep for [0.0s, 0.2s, 0.4s, …] between
    retries. 1 second the successive sleeps will be 0.5, 1, 2, 4, 8, 16, 32, 64, 128, 256. 2 seconds - 1, 2, 4,
    8, 16, 32, 64, 128, 256, 512 10 seconds - 5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560
    :return: Session object
    """
    session = requests.Session()
    retry = Retry(total=retries, backoff_factor=backoff_factor, method_whitelist=frozenset(['POST']))
    adapter = HTTPAdapter(max_retries=retry)
    session.mount('http://', adapter)
    return session

我将上述函数中的会话对象用作 session.post()

【问题讨论】:

    标签: python request urllib3


    【解决方案1】:

    会话将自动尝试尽可能长时间地保持活动状态(除非明确禁用),直到它关闭。任何超时都将由服务器强制执行。

    【讨论】:

    • 能否详细说明`timeout will be server enforces`是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多