Python, threads, and requests: What happens when I make concurrent requests in one session?

Python, threads, and requests: What happens when I make concurrent requests in one session? … here is a solution to the problem.

Python, threads, and requests: What happens when I make concurrent requests in one session?

Let’s say I use ThreadPoolExecutor via requests. Session makes 5 requests to the server:

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 use the same requests for each request. Session, do we have to wait for the server to acknowledge the first request before sending the next one?

If I have 5 sessions open at the same time (one session per thread), will I be able to send requests faster by sending each request through my own session?

The maintainers have recommended “one session per thread”, so it’s certainly doable… But will it improve performance?

Is it better to use aiohttp and async?

Solution

So, first of all, if you’re not sure if an object/function is thread-safe, you should assume it’s not. Therefore, you should not use the Session object in multiple threads without proper locking.

As for performance: always measure. Many libraries tend to do a lot behind the scenes, including opening multiple TCP connections. They may be configurable to tune performance, making it difficult to answer this question exactly. Especially since we don’t know about you. For example, if you plan to make 5 parallel requests, you only need to run 5 threads and 5 session objects. You most likely won’t see the difference between libraries (unless you’ve chosen a very bad one). On the other hand, it will be important if you are looking at hundreds or thousands of concurrent requests.

No matter what: always measure yourself.

Related Problems and Solutions