Python Apscheduler Loop Without Overlapping Tasks

Viewed 103

I'm trying to work with Apscheduler to run 3 tasks sequentially, but I'm having a challenge figuring out how to prevent overlapping of tasks before the whole cycle is completed.

import time
from apscheduler.schedulers.blocking import BlockingScheduler

def my_interval_job1():
    t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print ("interval job 1 --- {}".format(t))

def my_interval_job2():
    t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print ("interval job 2 --- {}".format(t))

def my_interval_job3():
    t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print ("interval job 3 --- {}".format(t))

def myScheduler():
    scheduler = BlockingScheduler({'apscheduler.timezone': 'UTC'})

    scheduler.add_job(my_interval_job1, 'interval', id='my_job_id1', seconds=5)
    scheduler.add_job(my_interval_job2, 'interval', id='my_job_id2', seconds=10)
    scheduler.add_job(my_interval_job3, 'interval', id='my_job_id3', seconds=15)

    try:
        scheduler.start()
    except KeyboardInterrupt:
        scheduler.shutdown()

myScheduler()

My output is like this:

interval job 1 --- 2022-01-08 13:59:43
interval job 1 --- 2022-01-08 13:59:48
interval job 2 --- 2022-01-08 13:59:48
interval job 1 --- 2022-01-08 13:59:53
interval job 3 --- 2022-01-08 13:59:53
interval job 1 --- 2022-01-08 13:59:58
interval job 2 --- 2022-01-08 13:59:58
interval job 1 --- 2022-01-08 14:00:03
interval job 1 --- 2022-01-08 14:00:08
interval job 2 --- 2022-01-08 14:00:08
interval job 3 --- 2022-01-08 14:00:08
interval job 1 --- 2022-01-08 14:00:13

How can i effectively loop through the three tasks and get an output like this:

------ Sleep (5secs)------    
interval job 1 --- 2022-01-08 13:59:43
------ Sleep (10secs)------
interval job 2 --- 2022-01-08 13:59:53
------ Sleep (15secs)------
interval job 3 --- 2022-01-08 14:00:08

------ Sleep (30secs)------

------ Sleep (5secs)------
interval job 1 --- 2022-01-08 14:00:43
------ Sleep (10secs)------
interval job 2 --- 2022-01-08 14:00:53
------ Sleep (15secs)------
interval job 3 --- 2022-01-08 14:01:08

And so on...

1 Answers

You can check other programs through RAM in run time. You can look at the Memory Map. Maybe it is not the answer which you looking for but if I were you I would use this method.

Related