python-apscheduler+ThreadPool-多线程定时任务

# -*- coding: utf-8 -*-
import re
import threading
import time
from multiprocessing.pool import ThreadPool

from apscheduler.schedulers.blocking import BlockingScheduler


# 一个设备一个线程
# 一个设备多个定时任务


def task_one(*args, **kwargs):
    for value in args:
        print(f"{threading.current_thread().ident}[{time.strftime('%Y-%m-%d %H:%M:%S')}]: task_one--------{value}")


def task_two(*args, **kwargs):
    a = kwargs["a"]
    b = kwargs["b"]
    print(
        f"{threading.current_thread().ident}[{time.strftime('%Y-%m-%d %H:%M:%S')}]: task_two--------{a}+{b}={sum([a, b])}")


def do_task(run_date: str, task: dict):
    date_pattern = re.compile(r"(.*)-(.*)-(.*) (.*):(.*):(.*)")
    year, month, day, hour, minute, second = date_pattern.search(run_date).groups()
    apscheduler = BlockingScheduler()
    for func_name, func_para_list in task.items():
        for func_para in func_para_list:
            args = func_para.get("args", ())
            func = globals()[f"task_{func_name}"]
            kwargs = func_para.get("kwargs", {})
            apscheduler.add_job(func=func, trigger="cron", args=args, kwargs=kwargs
                                , year=year
                                , month=month
                                , day=day
                                , hour=hour
                                , minute=minute
                                , second=second
                                )
    apscheduler.start()


if __name__ == '__main__':
    devices_map = {
        "one": [{"run_date": "*-*-* *:*:*",
                 "task": {"one": [{"args": ["A", "B"]}],
                          "two": [{"kwargs": {"a": 1, "b": 2}},
                                  {"kwargs": {"a": 1, "b": 3}}
                                  ]
                          }}]
    }
    pool = ThreadPool(len(devices_map))
    for item_list in devices_map.values():
        for item in item_list:
            run_date = item["run_date"]
            task = item["task"]
            pool.apply_async(do_task, kwds={"run_date": run_date, "task": task})
    pool.close()
    pool.join()
    # task = {"one": [{"args": ["A", "B"]}],
    #         "two": [{"kwargs": {"a": 1, "b": 2}},
    #                 {"kwargs": {"a": 1, "b": 3}}
    #                 ]
    #         }
    # do_task(run_date="*-*-* *:*:*/10", task=task)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值