4

Let's say I make 5 requests via a requests.Session to a server, using a ThreadPoolExecutor:

session = requests.Session()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)

def post(data):
    response = mysession.post('http://example.com/api/endpoint1', data)
    return response

for data in (data1, data2, data3, data4, data5):
    executor.submit(post, data)

Since we are using the same requests.Session for each request, do we have to wait for the server to acknowledge the first request before we can send the next one?

If I had 5 sessions open concurrently -- one session per thread -- would I be able to send the requests more rapidly by sending each request via its own session?

The maintainer already recommends "one session per thread" so it's certainly doable... but will it improve performance?

Would I be better off using aiohttp and async?

right2clicky
  • 785
  • 7
  • 14
  • First of all you should ask whether `Session` object is thread safe. Read this: https://stackoverflow.com/questions/18188044/is-the-session-object-from-pythons-requests-library-thread-safe It seems that it is not, so I would not do it. Secondly, one session per thread should not affect performance noticably (unless you are dealing with thousands concurrent requests). Finally, how about you simply measure performance for different variants (including async)? – freakish Feb 11 '18 at 14:28
  • Yes, I linked to a discussion of Session object thread safety in my post ("one session per thread"). Yes, measuring performance is one way to do it, but I was hoping to first learn how these things work ("do we have to wait for the server to ack") because blind testing is, well, blind. – right2clicky Feb 11 '18 at 14:31
  • Server is not acking back. Server is sending a response. HTTP has very limited request piping, especially over one connection. Anyway, how libraries handle HTTP connections may vary (do they use pool of connections under the hood?), so just measure it. – freakish Feb 11 '18 at 14:34
  • You should post that as an answer :) – right2clicky Feb 11 '18 at 14:37

1 Answers1

4

So, first of all if you are not sure whether certain object/function is thread safe you should assume that it is not. Therefore you should not use Session objects in multiple threads without appropriate locking.

As for performance: always measure. Many libraries tend to do lots of stuff under the hood, including opening multiple TCP connections. They can probably be configured to tune performance, so its very hard to answer the question precisely. Especially since we don't know your case. For example if you intend to make 5 parallel requests, then simply run 5 threads with 5 session objects. Most likely you won't see a diffrence between libs (unless you pick a really bad one). On the other hand if you are looking at hundreds or thousands concurrent requests it will matter.

Anyway: always measure it yourself.

freakish
  • 54,167
  • 9
  • 132
  • 169