# Post requests using threading

**URL:** <https://discuss.python.org/t/post-requests-using-threading/5638>\
**Category:** Python Help\
**Created:** [November 3, 2020, 1:48pm UTC](https://discuss.python.org/t/post-requests-using-threading/5638 "2020-11-03T13:48:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linto\_Kc](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/linto_kc/32/2825_2.png) [@Linto\_Kc](https://discuss.python.org/u/Linto_Kc)\
**Post date:** [November 3, 2020, 1:48pm UTC](https://discuss.python.org/t/post-requests-using-threading/5638/1 "2020-11-03T13:48:43Z")

</div>

is this a good practice. can it cause any problems

def post\_request(req\_data, header):  
requests.post(‘[http://127.0.0.1:8060/cv/rest/v2/analytics](http://127.0.0.1:8060/cv/rest/v2/analytics)’,  
json=req\_data, headers=header, timeout=20)

count = 1000  
i = 0  
while i \< count:  
headers = {‘Content-type’: ‘application/json’, ‘uid’: str(uuid.uuid1())}  
t1 = threading.Thread(target=post\_request, args=(data, headers), daemon=True)  
t1.start()  
i = i + 1

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [November 3, 2020, 8:53pm UTC](https://discuss.python.org/t/post-requests-using-threading/5638/2 "2020-11-03T20:53:09Z")

</div>

You’re doing 1000 of these. (a) you might run out of Threads, though  
probably well over 1000 and (b) you may exceed the capacity of the  
server you’re contacting and © depending on your other code, you may  
need some locking/access-control when you store or process the results  
of the post.

Usually one wants to limit the number of parallel connections. Even if  
the server copes, there will usually be a performance peak - up to that  
many connections increases throughput for you, beyondthat the server  
slows down and everyone loses. And of course most servers are shared, so  
you don’t want to prevent service to other users. So you may want a  
semaphore to limit the request calls, example:

```
from thrreading import Semaphore

S = Semaphore(16)

def post_request(req_data, header):
    with S:
        requests.post('http://127.0.0.1:8060/cv/rest/v2/analytics',
                      json=req_data, headers=header, timeout=20)

```

This doesn’t limit your threads, but no more than 16 requests.post calls  
will run at once.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [November 3, 2020, 11:25pm UTC](https://discuss.python.org/t/post-requests-using-threading/5638/3 "2020-11-03T23:25:21Z")

</div>

Instead of using primitives such as a Semaphore, you could use the `concurrent.futures` library:

```python
import uuid
from concurrent.futures import ThreadPoolExecutor, wait
import requests

executor = ThreadPoolExecutor(max_workers=16)
futures = []
for i in range(1000):
    future = executor.submit(
        requests.post,
        'http://127.0.0.1:8060/cv/rest/v2/analytics',
        json=data,
        headers={
            'Content-type': 'application/json',
            'uid': str(uuid.uuid1()),
        },
        timeout=20,
    )
    futures.append(future)

...
wait(futures)

```
