Double POST requests sent at the same time when a delay is encountered.

I have a code that sends GET and POST requests every 10 seconds. But in case there is a delay, the POST requests line itself posts twice. I tried to debug this to the best of my ability and I couldn't but finalize that the library sends the requests twice, or it is something to do with TCP (but then the library should deal with it, right? 

The code: # this is a general code to give an idea of my implementation 

import requests
import time
while True:
     source = requests.get(url)
     result = source.json()
     resp = request.post(another_url,json = result,headers) 
     time.sleep(10)

By default, this should be ok, but I don't understand why I get this : 

![1 4 2_clean_3git](https://cloud.githubusercontent.com/assets/11473261/8950185/8dc6db68-35f0-11e5-933c-bf0b7c49c0ef.PNG)

If it is something to do with my code then please advise me, or otherwise I hope this bug can be fixed. 

Thank you so much for this awesome library. 


Just to clarify: can you confirm you're not getting redirected? Check `resp.history` to see if there's anything in it.


Thank you for your answer sir. 

Actually I don't really know about this, I just included the resp.history and source.history into my code and I am getting [] [] for a few loops already. Maybe I need to leave it until that bug occurs and then I can will report back?!


That feels like it'd be useful if you can spare the time. =)


Definitely (Y). I will try to leave it running for as much as I can. Hopefully I can get it up for a day. Usually it happens a few times a day. 


![1 4 2_clean_4git](https://cloud.githubusercontent.com/assets/11473261/8975200/f21edc74-36ad-11e5-845c-c67cd601062a.PNG)

For this test, the database is supposed to receive 5 parameters each 10 seconds. The picture shows the times when I got 10 parameters in 1 second, which is the time when the server received double entries in the same second. I checked the data, and whenever the data received is doubled, it appears that it is actually just a duplicate, and has exactly the same values of the other entry at that specific second. 

Regarding the resp.history and source.history, I got them all as empty lists for each loop, even at the time when the problem occurred. 


Can I confirm you aren't using a custom Transport Adapter, and that you haven't changed any constants in `requests.adapters`?


I calculated the difference of the between my computer and the server and it is 1 minute an 43 seconds. This allowed me to find this: 

![1 4 2_clean_5git](https://cloud.githubusercontent.com/assets/11473261/8979733/63996f42-36de-11e5-949a-69c3ac39b1c8.PNG)

Let me go a bit further into my code:
![1 4 2_clean_git_delay](https://cloud.githubusercontent.com/assets/11473261/8977508/70d93708-36cc-11e5-85ba-d1780f31f161.PNG)
This is how I control the delay, but then I figured that instantly repeating the loop might cause this problem. so I put a delay under if in a previous test, but I still got the same thing. I am retesting again with a delay of 1 and 5 seconds under if to get more robust results. 

I am not using a custom Transport Adapter, and I have not changed any constants in `requests.adapters`


I edited my previous comment. Thank you. 


So, that code looks ... interesting. I'm going to guess that it looks something like

``` py
while True:
    start_time_loop = time.time()
    time.sleep(10)

    if time.time() - start_time_loop > 10:
        pass
    else:
        time.sleep(abs(10 - (time.time() - start_time_loop)))

    r = requests.post(...)
```

If I were you I'd rewrite this as

``` py
SLEEP_TIME = 10  # in seconds
while True:
    start_time_loop = time.time()
    time.sleep(SLEEP_TIME)

    time_slept = time.time() - start_time_loop
    if time_slept < SLEEP_TIME:
        time.sleep(SLEEP_TIME - time_slept)

    r = requests.post(...)
```

Basically, you're eliminating the usage of `pass` since you only want to do something if the difference is less than 10. That said, if you're anticipating that sleep could return early there is clearly something in here that you're not telling us about. Some kind of event library perhaps (eventlet, gevent, etc.) and depending on how you're using requests, this could be relevant.


Thank you so much for taking the time, sir. 

First of all, I want to apologize, this is actually my first issue here in github and I didn't know that github followed stackoverflow way, and that's why my code up there looks weird. I will rewrite it now. 

```
import requests
import time
while True:
start_time_loop = time.time() 
    source = requests.get(url)
    result = source.json()
    resp = request.post(another_url,json = result,headers) 

# in this situation, the loop might restart immediately resulting in an extra entry at the same second
    if time.time()-start_time_loop> 10: pass # there should be some small delay here, I guess! 
# if not, then just consider the processing time and anything else, like the delay in posting sometimes
    else : time.sleep(abs(10 - (time.time() - start_time_loop))) 
```

Your code actually gave me an idea, why not make the delay first? But then I remembered that I need to consider the time it takes to process all that including posting time ( I am GETting from a sensor, so there's no delay there, but I am POSTing to a server). And as I don't have a timeout as you may have noticed, that it is due to the fact that I want TCP to take over and control here in case there's some read timeout. Because apparently, sometimes when a read timeout occurs, and retry, what happens is that I will will create another connection, and when the server finally responds, all the connections created will be received at the same time. But that's another problem that has been solved by eliminating the timeout. 

Currently, I am trying to eliminate the double entries. It happened in the _comment_ where I mentioned the `requests.adapters`.

Btw, you can say that this is basically my code, I am only using `requests` and `time` libraries.  


I may have come across the reason why this is happening, which could mostly be a mistake from my part. Please don't spend more time on this. I will request for _topic closed_ once I verify. Please forgive me. 


No need to apologise: bug reports opened in good faith are always fine. =)


@Lukasa I wanna ask though, how is `requests` sending the POST requests? My friend has a C code that sends with me to the same table and I just noticed that in one situation, he was sending while my code suddenly stopped sending for around a minute. I am guessing that my code got stuck in one POST request for that minute before it went through. We are both connected to the same network. 

In this case, is there a way to make this more efficient? If I put a timeout, it will just create connections during that minute and then all of them will be received at the same time!


![amir_syn](https://cloud.githubusercontent.com/assets/11473261/9005452/eac92d5e-37b0-11e5-9e34-22d0e9ea992b.PNG)

This is an example from the database to what I mean. I am "1.5-PC" and he's the other one.


I found out that this issue has go nothing to do with the library. Thank you for your patience. I am still trying to figure out how to deal to the problem I mentioned in the last comment, should I open a new issue for it?


@itsHaddad you can use a timeout certainly. It looks like it would result in a connection error because connecting would timeout (perhaps). Please don't open an issue for how to properly use a timeout. StackOverflow can help you with that.


I understand. I tried the timeout previosuly but apparently in the case of a read timeout it would still keep trying to send for some time. Here @ www.python-requests.org/en/latest/api/#requests.Response.close it says `Note: Should not normally need to be called explicitly.`