Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Session object from Python's Requests library thread safe?

Python's popular Requests library is said to be thread-safe on its home page, but no further details are given. If I call requests.session(), can I then safely pass this object to multiple threads like so:

session = requests.session() for i in xrange(thread_count):     threading.Thread(         target=target,         args=(session,),         kwargs={}     ) 

and make requests using the same connection pool in multiple threads?

If so, is this the recommended approach, or should each thread be given its own connection pool? (Assuming the total size of all the individual connection pools summed to the size of what would be one big connection pool, like the one above.) What are the pros and cons of each approach?

like image 869
DJG Avatar asked Aug 12 '13 13:08

DJG


People also ask

Is requests library thread-safe?

A requests Session is documented as threadsafe but there are still a couple corner cases where it isn't perfectly threadsafe. The best way to use a Session is to use one per thread. The implementation provided by the toolbelt is naïve.

Are Python requests multithreaded?

Threading in Python is simple. It allows you to manage concurrent threads doing work at the same time. The library is called “threading“, you create “Thread” objects, and they run target functions for you. You can start potentially hundreds of threads that will operate in parallel.

Are objects thread-safe Python?

No, it is not thread safe - the two threads are essentially modifying the same variable simultaneously.

Are Python strings thread-safe?

Like the other have said, Python objects are mostly thread-safe. Although you will need to use Locks in order to protect an object in a place that require it to go through multiple changes before being usable again.


1 Answers

After reviewing the source of requests.session, I'm going to say the session object might be thread-safe, depending on the implementation of CookieJar being used.

Session.prepare_request reads from self.cookies, and Session.send calls extract_cookies_to_jar(self.cookies, ...), and that calls jar.extract_cookies(...) (jar being self.cookies in this case).

The source for Python 2.7's cookielib acquires a lock (threading.RLock) while it updates the jar, so it appears to be thread-safe. On the other hand, the documentation for cookielib says nothing about thread-safety, so maybe this feature should not be depended on?

UPDATE

If your threads are mutating any attributes of the session object such as headers, proxies, stream, etc. or calling the mount method or using the session with the with statement, etc. then it is not thread-safe.

like image 99
millerdev Avatar answered Sep 20 '22 04:09

millerdev