I am using time.sleep() to periodically check how many async calls have returned. I would like to know the best practice.
I am using async calls for certain services, and need to get the results back in certain time.
To simplify the discussion, let us just assume that a global variable num_calls_returned is used to track how many async calls have returned.
import datetime
import time
num_calls_returned = 0
start_time = datetime.datetime.now()
now = start_time
while num_calls_returned < 5 and now - start_time < datetime.timedelta(seconds=10):
time.sleep(0.1)
now = datetime.datetime.now()
The code above works, but I'd like to know the best practice. How bad is the code calling time.sleep()? Certainly the calculation of now and delta is a waste of CPU time, but I guess it's negligible.