And any time we save, add, remove, etc. jobs, we want to use the with construct to invoke and use the lock. For instance:
def remove_job(self, job_id: str):
"""Removes a job by its ID."""
with self.lock:
self.jobs = [job for job in self.jobs if job["id"] != job_id]
self._save_jobs()
An instance of the
JobManager
class is not thread-safe. This can lead to race conditions, deadlocks, etc.We can do this by making using of the
threading
module in Python. From there, we would add a lock variable to theJobManager
class:And any time we save, add, remove, etc. jobs, we want to use the
with
construct to invoke and use the lock. For instance:etc.