Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of mounting a Session object?

I have seen something like this in a few code snippets and in the Requests documentation:

import requests
sess = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=20)
sess.mount('https://', adapter)

I am trying to get a better sense of what .mount() does here. In this case, is it only to increase the number of allowed retries for all calls to sess.request()? Is it emulating something like:

for _ in range(max_retries):
    try:
        return sess.request(...)
    except:
        pass

or is there more going on?

I know that requests.Session instances are initialized with adapters that have max_retries=0, so the above is just a hunch based on that.

It would just be helpful to know how specifically .mount() is altering the session object's behavior in this case.

like image 454
Brad Solomon Avatar asked Jun 20 '18 02:06

Brad Solomon


1 Answers

.mount() really does what you think, it just mount a custom adapter to a given schema.

In your given example, it does just increase the number of allowed retries. But actually it can do more depending on what adapter is used.

For example, you can also change the connection pool size by HTTPAdapter(pool_maxsize=100). You can do some further customization by creating a total customized adapter such as MyHTTPAdapter.

The choice is given to you.

like image 72
Sraw Avatar answered Sep 28 '22 04:09

Sraw