Python计划,并行执行任务

时间:2018-02-27 10:02:12

标签: python schedule

我已使用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需要5 minutes才能投放,distance_fixer需要约5 hours。当我在运行distance_fixer时运行代码时,每个channel_crawler的计划都不会运行10 minutes。如何并行运行我的功能?

1 个答案:

答案 0 :(得分:2)

您可以对作业使用多处理,因此每个进程都运行每个函数

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()