Reputation: 23484
I recently found global request.Session()
declaration. E.g:
# 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.
Upvotes: 0
Views: 2167
Reputation: 21280
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 (e.g. in a HTTP server like Flask with uwsgi), you'll could end up with worker_threads*requests_pool_size
connections, which seems excessive.
Upvotes: 1