使用Lock保护状态变更,结合Condition和队列实现线程安全的状态机,确保多线程下状态切换的安全与逻辑清晰。

在多线程环境中实现状态机,核心是保证状态切换的安全性和逻辑清晰。Python 的 threading 模块提供了基础支持,但要控制复杂逻辑,需结合同步机制与良好的设计模式。
状态机本质是维护一个当前状态,并根据输入或事件进行转移。在多线程中,多个线程可能同时尝试触发状态转移,必须防止竞态条件。
通过 threading.Lock 保护状态变更操作,确保任意时刻只有一个线程能修改状态。
立即学习“Python免费学习笔记(深入)”;
import threading
<p>class ThreadSafeStateMachine:
def <strong>init</strong>(self):
self._state = "idle"
self._lock = threading.Lock()</p><pre class='brush:python;toolbar:false;'>def transition(self, event):
with self._lock:
if self._state == "idle" and event == "start":
self._state = "running"
print("State → running")
elif self._state == "running" and event == "stop":
self._state = "idle"
print("State → idle")
else:
print(f"Ignore event {event} in state {self._state}")
def get_state(self):
with self._lock:
return self._state某些场景下,线程需要等待状态达到某个条件才继续执行。例如:工作线程等待“运行”状态,主线程控制启停。
threading.Condition 可以让线程等待特定状态,状态变化后主动唤醒。
import time
<p>class ControlledWorker:
def <strong>init</strong>(self):
self.state = "paused"
self.condition = threading.Condition()</p><pre class='brush:python;toolbar:false;'>def worker_loop(self):
while True:
with self.condition:
while self.state == "paused":
self.condition.wait() # 等待被唤醒
if self.state == "stopped":
break
print("Working...")
time.sleep(1)
print("Worker stopped.")
def start(self):
with self.condition:
self.state = "running"
self.condition.notify_all()
def pause(self):
with self.condition:
self.state = "paused"
def stop(self):
with self.condition:
self.state = "stopped"
self.condition.notify_all()对于更复杂的逻辑,可引入 queue.Queue 作为事件通道。状态机从队列中消费事件,避免多线程直接调用带来的混乱。
这种方式解耦了事件产生与处理,适合高并发或异步任务场景。
import queue
import threading
<p>def event_driven_fsm():
fsm_queue = queue.Queue()
state = "idle"</p><pre class='brush:python;toolbar:false;'>def fsm_worker():
nonlocal state
while True:
event = fsm_queue.get()
if event == "quit":
break
with threading.Lock(): # 状态本身仍需保护
if state == "idle" and event == "init":
state = "ready"
elif state == "ready" and event == "run":
state = "running"
elif event == "reset":
state = "idle"
print(f"[FSM] State: {state}, Event: {event}")
fsm_queue.task_done()
threading.Thread(target=fsm_worker, daemon=True).start()
return fsm_queueq = event_driven_fsm() q.put("init") q.put("run") q.put("reset") q.put("quit") q.join() # 等待处理完成
基本上就这些。关键是把状态变更变成串行化操作,再通过合适的同步原语协调线程行为。结构清晰了,复杂逻辑也能可控。
以上就是Python多线程如何实现状态机 Python多线程复杂逻辑控制技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号