Files
pig-house-controller/app/worker.py
2025-10-13 14:35:20 +08:00

45 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
工作线程模块
职责:
- 作为一个独立的线程运行。
- 阻塞式地等待任务队列。
- 从队列中取出任务并交给Processor进行耗时处理。
"""
from app.uqueue import Queue
from app.processor import Processor
from app.logs.logger import log
def worker_task(task_queue: Queue, processor: Processor):
"""
工作线程的主函数。
Args:
task_queue (uqueue.Queue): 共享的任务队列。
processor (Processor): 业务处理器实例。
"""
log("工作线程已启动,等待任务...")
while True:
try:
# 1. 阻塞式地从队列中获取任务
# 如果队列为空程序会在这里自动挂起不消耗CPU
# get()方法是线程安全的
packet_bytes = task_queue.get()
log(f"工作线程:收到新任务,开始处理... 数据: {packet_bytes.hex()}")
# 2. 调用processor进行耗时的、阻塞式的处理
# 这个处理过程不会影响主线程的LoRa监听
processor.handle_packet(packet_bytes)
log("工作线程:任务处理完毕,继续等待下一个任务。")
except Exception as e:
log(f"错误:工作线程在处理任务时发生异常: {e}")