Search code examples
juliascheduling

What is the Julia equivalent to Python's schedule package?


I have a piece of Python code which a want to convert to Julia. I the python code I use the schedule package. What is the equivalent in Julia, I looked at the "Tasks and Parallel Computing" part in the Julia documentation but I can't find something similar. The code in Python is:

def main():
    schedule.every(0.25).seconds.do(read_modbus, 1, 1000, 100, 1)
    while True:
        schedule.run_pending()
        time.sleep(0.05)

Solution

  • Will a Timer work? This form of Timer calls your function in a Task so you need to yield control from you main loop occasionally to allow the timer task to run. You can yield by calling yield, sleep, wait, or doing IO, here I show waiting on the timer.

    tstart = time()
    ncalls = 0
    read_modbus() = (global ncalls+=1;@show (time()-tstart)/ncalls,ncalls)
    t=Timer((timer)->read_modbus(),0,0.25)
    
    while true
        wait(t) # wait for timer to go off
        println("mainloop $ncalls")
    end