Emad Helmi
Emad Helmi

Reputation: 695

Python schedule, do tasks in parallel

I have scheduled my tasks using this:

if __name__ == '__main__':
    channel_crawler(priority=1)
    schedule.every(PRIORITY[1]["interval"]).minutes.do(channel_crawler, priority=1)

    schedule.every().day.at("17:30").do(distance_fixer)

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

channel_crawler takes about 5 minutes to run and distance_fixer takes about 5 hours. when I run my code while running the distance_fixer, the schedule does not run channel_crawler every 10 minutes. How can I run my functions in parallel?

Upvotes: 1

Views: 2802

Answers (1)

patpat
patpat

Reputation: 704

You can use multiprocessing for the job, so each process run each function

def run_schedule():
    while True:
        schedule.run_pending()
        time.sleep(1)

def run_crawler_schedule():
    channel_crawler(priority=1)
    schedule.every(PRIORITY[1]["interval"]).minutes.do(channel_crawler, priority=1)
    run_schedule(schedule)

def run_fix():
    schedule.every().day.at("17:30").do(distance_fixer)
    run_schedule()


def run_job()
    p = Process(target=run_crawler_schedule)
    c = Process(target=run_fix)
    p.start()
    c.start()
    p.join()
    c.join()

if __name__ == "__main__":
    run_job()

Upvotes: 6

Related Questions