User
User

Reputation: 24759

How to close connections in Requests (scalability)

What is the equivalent of this, using the Requests library?

with contextlib.closing(urllib2.urlopen(request)) as response:
    return response.read().decode('utf-8')

Requests seems to be a more modern solution for 2.7 than urllib2.

What would be the correct way to close the connections, so running the function 800 times or so, won't leave connections open and cause performance issues? Should I create a session and use close() on it?

Is it like this?

s = requests.Session()
s.get('http://httpbin.org/get')
s.close()

Upvotes: 0

Views: 483

Answers (1)

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28845

There should be no connections open if you use requests in a very basic way. If you start using stream=True you will need to concern yourself with whether or not the entire response has been read, and whether or not it should then be closed. Otherwise, this should never be a concern of yours.

Upvotes: 1

Related Questions