python定时检查,多线程执行任务,定时关闭等功能源码

这是一个python写的定时检查文件中的数据,当存在数据时运用多线程进行处理,并且可以设置运行时间,在到达指定时间后发送退出指令,达到定时退出的功能。

这是一个小尝试,也是学习的一小步

import os
import time
from datetime import datetime, timedelta
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
from concurrent.futures import ThreadPoolExecutor
import threading
import uuid

DATA_FILE = 'data.txt'
executor = ThreadPoolExecutor(max_workers=2)
tasks = {}
scheduler = BackgroundScheduler()
scheduler.start()

def process_data():
    if not os.path.exists(DATA_FILE):
        return
    
    with open(DATA_FILE, 'r+') as f:
        lines = f.readlines()
        f.seek(0)
        f.truncate()

        for line in lines:
            data = eval(line.strip())
            task_id = str(uuid.uuid4())  # 生成唯一任务ID
            stop_event = threading.Event()
            tasks[task_id] = {
                'data': data,
                'future': executor.submit(execute_task, data, task_id, stop_event),
                'stop_event': stop_event,
                'started': False  # 标记任务是否已经开始
            }

def execute_task(data, task_id, stop_event):
    try:
        print(f"Executing task: {data} (ID: {task_id})")
        tasks[task_id]['started'] = True
        # 调用 schedule_stop 设置停止时间
        schedule_stop(task_id)
        # 模拟长时间运行的任务
        while not stop_event.is_set():
            time.sleep(100)  # 模拟任务的执行
            print(f"Task {task_id} is running...{data}")
    finally:
        # 任务结束后删除任务
        if task_id in tasks:
            del tasks[task_id]

def stop_task(task_id):
    if task_id in tasks and tasks[task_id]['started']:
        tasks[task_id]['stop_event'].set()
        print(f"Stopping task: {task_id}")
        if task_id in tasks:
            del tasks[task_id]

def schedule_stop(task_id):
    run_date = datetime.now() + timedelta(seconds=3)
    scheduler.add_job(stop_task, 'date', run_date=run_date, args=[task_id])

# 每隔1分钟检查文件并处理数据
scheduler.add_job(process_data, IntervalTrigger(minutes=1))

# 保持主线程运行
try:
    while True:
        time.sleep(2)
except (KeyboardInterrupt, SystemExit):
    scheduler.shutdown()
    executor.shutdown(wait=False)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值