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.