美文网首页
定时周一到周五 多线程处理任务

定时周一到周五 多线程处理任务

作者: 是东东 | 来源:发表于2021-06-25 13:50 被阅读0次
import datetime
import threading
import time
import pandas as pd
import schedule


def get_current_week():
    """获取本周周一及周六日期"""
    monday, sunday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while monday.isoweekday() != 1:
        monday -= one_day
    while sunday.isoweekday() != 6:
        sunday += one_day
    return pd.to_datetime(monday), pd.to_datetime(sunday)


def run(name, timed, num=''):
    time.sleep(timed)
    print(f'{fme} {num}')


def run_threaded(job_func, name, timed, num):
    job_thread = threading.Thread(target=job_func, args=(name, timed, num))
    job_thread.start()


if __name__ == '__main__':

    """定时"""

    start_time = '13:45:50'
    print(f'开启时间:{datetime.datetime.today()}\n{__file__.split("/")[-1]}\t{start_time} 自动抓取')
    schedule.every().days.at(start_time).do(run_threaded, run, 'job_thread1', 4, 200)
    schedule.every().days.at(start_time).do(run_threaded, run, 'job_thread2', 1, 1)
    while True:
        month, saturday = get_current_week()
        if month < datetime.datetime.today() < saturday:
            schedule.run_pending()  # 运行所有可运行的任务
        time.sleep(1)

相关文章

网友评论

      本文标题:定时周一到周五 多线程处理任务

      本文链接:https://www.haomeiwen.com/subject/rifoyltx.html