Your current approach of checking the elapsed time at each iteration and breaking the loop when it exceeds a threshold is a valid way to ensure that the job checking process doesn't exceed a certain amount of time.
However, if you want to ensure that each call to the update function doesn't exceed a certain amount of time, you might want to reconsider the approach of randomly shuffling the jobs. If the time to process each job is highly variable, this could mean that the total time taken in one call to update is very different from the next, even if the same number of jobs is processed.
One approach could be to implement a round-robin or a priority scheduling, where each job gets a fair chance to be processed in each call to update.
Here's an example of how you could implement round-robin scheduling:
class Allocator:
def __init__(self):
self.jobs = {}
self.job_keys = deque()
def add_job(self, job_key, job):
self.jobs[job_key] = job
self.job_keys.append(job_key)
def check_and_update_jobs(self):
t_start = time.perf_counter()
jobs_processed = 0
while self.job_keys:
job_key = self.job_keys.popleft()
job = self.jobs[job_key]
self.check_and_update_job(job)
jobs_processed += 1
if (time.perf_counter() - t_start) > MAX_TIME_CHECK_JOB_SEC:
# If we didn't finish processing this job, put it back at the end of the queue
self.job_keys.append(job_key)
break
This way, even if the update function is called repeatedly, each job will get its turn to be processed. If a job isn't finished processing when the time limit is reached, it will be the first to be processed the next time the update function is called. This can help to ensure fairness among jobs and prevent any one job from monopolizing the processing time.
Your current approach of checking the elapsed time at each iteration and breaking the loop when it exceeds a threshold is a valid way to ensure that the job checking process doesn't exceed a certain amount of time.
However, if you want to ensure that each call to the update function doesn't exceed a certain amount of time, you might want to reconsider the approach of randomly shuffling the jobs. If the time to process each job is highly variable, this could mean that the total time taken in one call to update is very different from the next, even if the same number of jobs is processed.
One approach could be to implement a round-robin or a priority scheduling, where each job gets a fair chance to be processed in each call to update.
Here's an example of how you could implement round-robin scheduling:
This way, even if the update function is called repeatedly, each job will get its turn to be processed. If a job isn't finished processing when the time limit is reached, it will be the first to be processed the next time the update function is called. This can help to ensure fairness among jobs and prevent any one job from monopolizing the processing time.