简体   繁体   中英

Global requests.Session object drawbacks

I recently found global request.Session() declaration. Eg:

# one of many files
import requests

g_session = requests.Session()

def some_foo():
    return g_session.post('https://example.com', data={'key': 'value'}

# rest of the code

From docs :

The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will use urllib3's connection pooling. So if you're making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase (see HTTP persistent connection).

So as i understand:

the benefits are no need to open new connection, and it can be reused

the downsides are supporting the same connection and keeping memory occupied


Is there anything else?

I've never seen such global declaration. Most of the times sessions are used in context manager way, or reuse object in same function/block code, but not globally.

Possible relevant info: it's part of django application.

One drawback is that it is unclear whether Session is thread-safe, since the requests documentation does not mention it. A thread from 2013 suggests that it is not: Is the Session object from Python's Requests library thread safe? but it links an Urllib3 issue that has been resolved and closed: PoolManager is not thread-safe

You could make your session thread-local, but if you are accessing your Session from worker threads (eg in a HTTP server like Flask with uwsgi), you'll could end up with worker_threads*requests_pool_size connections, which seems excessive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM