I am trying to run a certain function “foo” every second. I have to do this for a few minutes (say 5).
The function foo() makes 100 HTTP Requests (which contains a JSON object) to the server and prints the JSON response.
In short, I have to make 100 HTTP requests per second for 5 minutes.
I have just started learning python, thus don’t have extensive knowledge. This is what I have tried:
import threading
noOfSecondsPassed = 0
def foo():
global noOfSecondsPassed
# piece of code which makes 100 HTTP requests (I use while loop)
noOfSecondsPassed += 1
while True:
if noOfSecondsPassed < (300) # 5 minutes
t = threading.Timer(1.0, foo)
t.start()
Due to multiple threads, the function foo isn’t called 300 times but much much more than that.
I have tried setting a lock too:
def foo():
l = threading.Lock()
l.acquire()
global noOfSecondsPassed
# piece of code which makes 100 HTTP requests (I use while loop)
noOfSecondsPassed += 1
l.release()
Rest of code is same as the previous code snippet. But this also does not work.
How do I do this?
Edit: Different Approach
I have tried this approach which worked for me:
def foo():
noOfSecondsPassed = 0
while noOfSecondsPassed < 300:
#Code to make 100 HTTP requests
noOfSecondsPassed +=1
time.sleep(1.0)
foo()
Any disadvantages in doing so?
I would use another approach which is easier I think.
Create 300 timer thread, each running 1 sec after the previous. The main loop is executed in almost an instant so the error factor is very low.
Here’s a sample Demo:
This code output should look like this:
As you can see, each thread is launched about 1 sec after the previous and you are starting exactly 300 threads.