Permalink
Browse files

Put requests in a session to benefit from connection reuse

http://docs.python-requests.org/en/master/user/advanced/#session-objects
- Remove Connection: close, urllib3 will handle that
- Add class property session <= request.Session()
- And of course, request using this session when possible (still using request for fake headers creation, no method available in request.session to do that : https://github.com/kennethreitz/requests/blob/master/requests/sessions.py#L411)

Some numbers :
* without reuse : 0m58s for 32,4 Mo ; 8m00s for 287,4 Mo
* with reuse : 0m42s for 32,4 Mo ; 0m51s for 32,4 Mo ; 5m53s for 287,4 Mo
# Conflicts:
#	flickrapi/auth.py
  • Loading branch information...
Alex131089 authored and sybrenstuvel committed Dec 6, 2016
1 parent f3fec21 commit cdeb6ffea267d2dcba951417371984c8bcf48a21
Showing with 7 additions and 8 deletions.
  1. +7 −8 flickrapi/auth.py
@@ -149,6 +149,8 @@ def has_level(self, access_level):
class OAuthFlickrInterface(object):
"""Interface object for handling OAuth-authenticated calls to Flickr."""

session = requests.Session()

REQUEST_TOKEN_URL = "https://www.flickr.com/services/oauth/request_token"
AUTHORIZE_URL = "https://www.flickr.com/services/oauth/authorize"
ACCESS_TOKEN_URL = "https://www.flickr.com/services/oauth/access_token"
@@ -245,10 +247,9 @@ def do_request(self, url, params=None):
@return: the response content
"""

req = requests.post(url,
req = self.session.post(url,
data=params,
auth=self.oauth,
headers={'Connection': 'close'})
auth=self.oauth)

# check the response headers / status code.
if req.status_code != 200:
@@ -277,8 +278,7 @@ def do_upload(self, filename, url, params=None, fileobj=None):
# 1. create a dummy request without 'photo'
# 2. create real request and use auth headers from the dummy one
dummy_req = requests.Request('POST', url, data=params,
auth=self.oauth,
headers={'Connection': 'close'})
auth=self.oauth)

prepared = dummy_req.prepare()
headers = prepared.headers
@@ -289,10 +289,9 @@ def do_upload(self, filename, url, params=None, fileobj=None):
params['photo'] = ('dummy name', fileobj)
m = MultipartEncoder(fields=params)
auth = {'Authorization': headers.get('Authorization'),
'Content-Type': m.content_type,
'Connection': 'close'}
'Content-Type': m.content_type}
self.log.debug('POST %s', auth)
req = requests.post(url, data=m, headers=auth)
req = self.session.post(url, data=m, headers=auth)

# check the response headers / status code.
if req.status_code != 200:

0 comments on commit cdeb6ff

Please sign in to comment.