Python threading.Event详解:线程同步与超时处理实战

阿麦
阿麦
发布时间:2026-10-13| 2人浏览过
原创

本文深入解析Python中threading.Event类的用法,通过Redis服务连接场景演示如何协调多个工作线程。重点讲解Event的set与wait机制,以及利用wait方法的timeout参数实现带日志反馈的超时重试逻辑,帮助开发者掌握高效的线程同步策略。

threading.Event机制概述

threading.Event机制提供了一种线程间通信的方式,类似于一个主控线程向其他多个工作线程发送指令的模式。所有参与的工作线程都持有一个threading.Event对象实例,并等待该事件被触发。如果事件一直未被触发,这些线程将处于阻塞状态,直到事件真正发生为止。

为了说明这一机制,我们可以设想一个典型的应用场景:假设有多个线程需要从Redis队列中读取数据进行处理。这些线程在启动时都需要尝试连接Redis服务。通常情况下,如果连接失败,每个线程的代码逻辑中都会包含重试连接的机制。

Redis连接协调场景

如果我们希望在启动阶段确保Redis服务处于正常状态,再允许工作线程去连接Redis服务器,就可以利用threading.Event机制来统一协调各个工作线程的连接行为。

具体流程如下:主线程首先尝试连接Redis服务。如果连接成功,主线程会触发事件信号;随后,各工作线程接收到信号后,再尝试连接Redis服务器执行具体任务。

基础实现代码与运行结果

基于上述逻辑,我们可以编写如下的示例程序:


import threading
import time
import logging
 
logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s',)
 
def worker(event):
  logging.debug('Waiting for redis ready...')
  event.wait()
  logging.debug('redis ready, and connect to redis server and do some work [%s]', time.ctime())
  time.sleep(1)
 
readis_ready = threading.Event()
t1 = threading.Thread(target=worker, args=(readis_ready,), name='t1')
t1.start()
 
t2 = threading.Thread(target=worker, args=(readis_ready,), name='t2')
t2.start()
 
logging.debug('first of all, check redis server, make sure it is OK, and then trigger the redis ready event')
time.sleep(3) # simulate the check progress 
readis_ready.set()

运行该程序后的效果如下:


(t1    ) Waiting for redis ready...
(t2    ) Waiting for redis ready...
(MainThread) first of all, check redis server, make sure it is OK, and then trigger the redis ready event
(t2    ) redis ready, and connect to redis server and do some work [Wed Nov 5 12:45:03 2014]
(t1    ) redis ready, and connect to redis server and do some work [Wed Nov 5 12:45:03 2014]

在程序初始阶段,t1和t2线程均阻塞在等待Redis服务器启动的逻辑上。一旦主线程确认Redis服务已正常启动,便会触发redis_ready事件,此时各个工作线程解除阻塞,开始连接Redis并执行相应的工作。

利用超时参数处理等待

threading.Event的wait方法支持接收一个超时参数。默认情况下,如果事件一直未发生,wait方法会无限期地阻塞线程。然而,引入超时参数后,如果阻塞时间超过了设定的阈值,wait方法将返回,从而允许线程进行后续处理。

带日志反馈的超时重试方案

回到前面的应用场景,如果Redis服务器一直未启动,我们希望子线程能够打印日志以持续提醒当前没有可用的Redis服务。通过设置超时参数,我们可以实现这一目标。


import threading
import time
import logging
 
logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s',)
 
def worker(event):
  while not event.is_set():
    logging.debug('Waiting for redis ready...')
    event.wait(1)
  logging.debug('redis ready, and connect to redis server and do some work [%s]', time.ctime())
  time.sleep(1)
 
readis_ready = threading.Event()
t1 = threading.Thread(target=worker, args=(readis_ready,), name='t1')
t1.start()
 
t2 = threading.Thread(target=worker, args=(readis_ready,), name='t2')
t2.start()
 
logging.debug('first of all, check redis server, make sure it is OK, and then trigger the redis ready event')
time.sleep(3) # simulate the check progress 
readis_ready.set()

与之前无限阻塞的版本不同,这里在工作线程中增加了一个while循环。只要redis_ready事件未被触发,循环就会继续。每次调用wait方法时,设置1秒的超时时间。这样,工作线程在等待redis_ready的同时,会定期记录状态信息,直到事件最终被触发。

该程序的运行结果如下所示:


(t1    ) Waiting for redis ready...
(t2    ) Waiting for redis ready...
(MainThread) first of all, check redis server, make sure it is OK, and then trigger the redis ready event
(t2    ) Waiting for redis ready...
(t1    ) Waiting for redis ready...
(t2    ) Waiting for redis ready...
(t1    ) Waiting for redis ready...
(t2    ) redis ready, and connect to redis server and do some work [Wed Nov 5 13:55:46 2014]
(t1    ) redis ready, and connect to redis server and do some work [Wed Nov 5 13:55:46 2014]

通过这种方式,我们可以在等待Redis服务启动的过程中,清晰地观察到工作线程的等待状态及日志输出。

以上关于Python中使用threading.Event协调线程运行的详解内容,旨在为读者提供参考。希望这些信息能对您的多线程编程有所帮助,也欢迎大家继续支持本站获取更多技术分享。

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。