scheduling – How can I run a Python update-able job scheduler asynchronously?

I’d like to run a job scheduler concurrently with/to the Python main thread. The jobs have to be executed with a relative delay in the sub-second region. In addition I want to update jobs (the scheduler job queue) after the scheduler has been started. I need something similar to the code example below (here job delay of 500ms) but with the scheduler running non-blocking and wrapped into a class which provides a method to update jobs. The example uses the standard-lib scheduler sched. How can I emulate this functionality with threading and queue or another library I don’t know yet?

NOTE: It’s possible to call the asynchronous variant of the scheduler with which should allow continuous updates of jobs in the while loop in a procedural manner. However this would not allow to explicitly pass jobs to the scheduler e.g. from code in another module.


import sched
from time import time, sleep

def job(a='default'):
    print("Job executed:", time(), a)

s = sched.scheduler()
s.enter(0.5, 1, job, kwargs={'a': 'fish'})
s.enter(0.5, 1, job, kwargs={'a': 'blub'})
while True:
    s.enter(0.25, 1, job, kwargs={'a': 'BLUB'})

Job executed: 1602507911.0258906 fish
Job executed: 1602507912.0259538 blub
Job executed: 1602507913.0284517 BLUB
Job executed: 1602507914.0311859 BLUB
Job executed: 1602507915.0344074 BLUB