Somdeb Mukherjee
Somdeb Mukherjee

Reputation: 178

Does Python Schedule wait for code to finish?

I want to trigger a code using schedule module as follows:

# This is the function that I want to schedule and this function is calling a script file that does the actual processing. 

def run_codefile():
    print("LOG:: Running code file...")
    ret = os.system(f"{v_venv_path} {v_script_dir}")
    if ret != 0 or ret > 0:
        print("LOG:: ERROR!! In this iteration, some processing has failed. Please Check Log...")
    else:
        print("LOG:: This iteration, Job Completed Successfully...")
        print("LOG:: Initiating Log Cleanup...")
        ret = os.system(f"{v_venv_path} {v_log_cleanup_script}")
        if ret != 0 or ret > 0:
            print("LOG:: Log Cleanup Failed...")
        else:
            print("LOG:: Log Cleanup Completed Successfully...")
            print("LOG:: Sleeping Until Next iteration")

Then, I schedule the code as follows:

def schedule_jobs():
    schedule.every(2).hours.do(run_codefile)

My question is about this snippet that I am using to trigger the actual process:

ret = os.system(f"{v_venv_path} {v_script_dir}")

Will the schedule module actually wait to the file to complete execution and capture the return code for execution ? or Will the schedule module just trigger the code and return without waiting for the actual execution to complete? If the answer is Yes, then How do I implement a sequential trigger of few scripts and schedule it using python schedule module?

Upvotes: 0

Views: 960

Answers (1)

Abdo Bakry
Abdo Bakry

Reputation: 11

Schedule does not account for the time it takes for the job function to execute. To guarantee a stable execution schedule you need to move long-running jobs off the main-thread.

Thats mean it schedule will wait till your function finish its execution .

if you want parallel execution just take a look to https://schedule.readthedocs.io/en/stable/parallel-execution.html

Upvotes: 1

Related Questions