I facing a problem. I found a solution I'm explaining below but I want some advice aout to right way to resolve it.
Here is the problem:
I have a class object called Item. This Item has a method, call make_request which makes a GET request on a server, and save the result. Now, I have implemented 3 Item object which calls make_request. The Item objects gonna call the method each 3 minutes, but these make_requests must be delayed by 1 minutes from the previous object's call.
Example :
- 14:00 - Item0.make_request
- 14:01 - Item1.make_request
- 14:02 - Item2.make_request
- 14:03 - Item0.make_request
- 14:04 - Item1.make_request
- 14:05 - Item2.make_request
- 14:06 - Item0.make_request
- 14:07 - Item1.make_request
- 14:08 - Item2.make_request
- 14:09 - Item0.make_request
- 14:10 - Item1.make_request
- 14:11 - Item2.make_request
- 14:12 - Item0.make_request
- 14:13 - Item1.make_request
- 14:14 - Item2.make_request ... etc
What I do actully is a while loop where I check the time minute and call the right object's method.
from datetime import datetime
Item0 = Item(name="Item0")
Item1 = Item(name="Item1")
Item2 = Item(name="Item2")
while True:
if str(datetime.now().time.minute[-1]) in ['0', '3', '6']:
Item0.make_request()
if str(datetime.now().time.minute[-1]) in ['1', '4', '7']:
Item1.make_request()
if str(datetime.now().time.minute[-1]) in ['2', '5', '8']:
Item2.make_request()
This is a solution, but it's not clean and I don't like it. It is also missing 1 minute. I was thinking about using Queue.
I'm waiting for your advice :)
EDIT : something more robust
Thank a lot for your answers. I found a good solution for the first problem.
Now, I'm asking myself if I can, in the same context, use a Queue. The idea is to call the make_request independently of the result of the previous make_request.
For example: At 14:00:00 I call Item0.make_request. Unfortunately, it takes more than 60 seconds to get the result of Item0.make_request but I want my Item1.make_request to be called independently at 14:01:00.
It happens sometimes