这是一个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)