requests.session 闲置会过期吗?
Does requests.session expires if remained idle?
我在重试请求函数中使用 request.session()。我想知道 session 持续多长时间。如果 session
object 闲置一段时间,它会在一段时间后过期吗?
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 object 用作 session.post()
会话将自动尝试尽可能长时间地保持活动状态(除非明确禁用),直到它关闭。任何超时都将由服务器强加。
我在重试请求函数中使用 request.session()。我想知道 session 持续多长时间。如果 session
object 闲置一段时间,它会在一段时间后过期吗?
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 object 用作 session.post()
会话将自动尝试尽可能长时间地保持活动状态(除非明确禁用),直到它关闭。任何超时都将由服务器强加。