I have two jobs in APScheduler which are executed by the cron trigger. The read job (get_value_job
) reads a value from an external device and stores it in a variable. The write job (write_value
) reads this value every minute and will write it to a text file. If both jobs run concurrently at second 0, the value of the write job will be outdated, because of a small delay in the read job. So the writing job should only start if the read job is finished and the value is actual.
My current code uses a flag and a while loop. Is there a better solution without a while loop to force write_value_job
to wait until the value is updated?
class MyJob:
value = 0
finished = False
def get_value(self):
self.finished = False
# This job will be blocked for a few seconds, because we request
# an external device. Simulate it with delay of 5 seconds.
time.sleep(5)
self.value = 255
self.finished = True
def write_value(self):
while not self.finished:
time.sleep(0.1)
my_value = self.value
print(my_value)
if __name__ == '__main__':
scheduler = BackgroundScheduler()
job = MyJob()
scheduler.add_job(job.get_value, 'cron', id="get_value_job", second='*/10')
scheduler.add_job(job.write_value, 'cron', id="write_value_job", minute='*/1')
scheduler.start()