Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait the job ends before starts again #211

Open
dudumiquim opened this issue Apr 26, 2018 · 1 comment
Open

Wait the job ends before starts again #211

dudumiquim opened this issue Apr 26, 2018 · 1 comment

Comments

@dudumiquim
Copy link

I'm looking for a method to schedule a job to run every 2 seconds but without to start another thread while the first one is running yet.
I read the FAQ and the issues but no one comment about it.

import threading
import time
import schedule

def job():
    print("A: %s"  % threading.get_ident(), time.time())
    time.sleep(3)
    print("B: %s"  % threading.get_ident(), time.time())
    
def run_threaded(job_func):
    job_thread = threading.Thread(target=job_func)
    job_thread.start()

schedule.every(2).seconds.do(run_threaded, job)

while 1:
    schedule.run_pending()
    time.sleep(1)

The output is:

A: 139822667417344 1524702860
A: 139822659024640 1524702862
B: 139822667417344 1524702863
A: 139822667417344 1524702864
B: 139822659024640 1524702865
A: 139822659024640 1524702866
B: 139822667417344 1524702867
A: 139822667417344 1524702868

But, I would like something like it:

A: 139822667417344 1524702860
B: 139822667417344 1524702863
A: 139822667417344 1524702865
B: 139822667417344 1524702868

The every(2) start to count after the job ends.

@gkovacs81
Copy link

I think this should solve your problem:
...
job_thread.start()
job_thread.join()
...

My question is: how to wait for the threaded jobs in the main process to finish after a keyboard interrupt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants