Python 多线程详解(概念、初始化方式、线程间变量传递、线程锁以及一些注意事项)

Python 多线程详解

1. 多线程的概念

在 Python 中,多线程是指在一个进程内同时运行多个线程,以便在某些场景下提升程序的响应能力或并发性。

线程是 CPU 调度的最小单位,而进程是系统资源分配的最小单位。一个进程可以包含多个线程,这些线程共享同一进程的资源(包括内存空间)。

注意 :Python 的 GIL(Global Interpreter Lock,全局解释器锁) 限制了同一时刻只能有一个线程执行 Python 字节码 ,所以多线程在计算密集型任务中无法真正实现并行,更多用于 I/O 密集型任务(例如网络请求、文件读写)。


2. 多线程的初始化

在 Python 中可以使用 threading 模块来创建和管理线程。

2.1 使用 threading.Thread

python 复制代码
import threading
import time

def worker(name):
    print(f"线程 {name} 开始工作")
    time.sleep(1)
    print(f"线程 {name} 工作结束")

# 创建线程对象
t1 = threading.Thread(target=worker, args=("A",))
t2 = threading.Thread(target=worker, args=("B",))

# 启动线程
t1.start()
t2.start()

# 等待线程结束
t1.join()
t2.join()

print("所有线程执行完毕")

关键参数

  • target:线程运行的函数。
  • args:传给函数的参数(元组形式)。
  • kwargs:传给函数的关键字参数。

3. 线程之间的变量传递

3.1 共享变量

线程之间可以共享同一个全局变量(因为它们在同一个进程中),但这会引发 数据竞争 问题。

python 复制代码
import threading

counter = 0  # 全局变量

def increment():
    global counter
    for _ in range(100000):
        counter += 1  # 多线程这里可能会出错

threads = []
for _ in range(5):
    t = threading.Thread(target=increment)
    threads.append(t)
    t.start()

for t in threads:
    t.join()

print("counter =", counter)

上面的代码可能不会得到想象的结果,因为多个线程同时修改 counter 会产生 竞态条件


4. 线程锁(Lock)

为避免数据竞争,我们需要使用 (Lock)来确保某段代码同一时刻只有一个线程能执行。

python 复制代码
import threading

counter = 0
lock = threading.Lock()

def safe_increment():
    global counter
    for _ in range(100000):
        with lock:  # 自动 acquire/release
            counter += 1

threads = []
for _ in range(5):
    t = threading.Thread(target=safe_increment)
    threads.append(t)
    t.start()

for t in threads:
    t.join()

print("counter =", counter)  # 结果正确

4.1 Lock 的用法

python 复制代码
lock = threading.Lock()

# 方式1
lock.acquire()
try:
    # 临界区代码
    pass
finally:
    lock.release()

# 方式2 (推荐)
with lock:
    # 临界区代码
    pass

5. 线程间通信

除了共享变量,还可以使用 队列(Queue) 来安全地进行线程间数据传递。

python 复制代码
import threading
import queue
import time

q = queue.Queue()

def producer():
    for i in range(5):
        q.put(i)
        print(f"生产数据 {i}")
        time.sleep(0.2)

def consumer():
    while True:
        item = q.get()
        if item is None:  # 遇到 None 退出
            break
        print(f"消费数据 {item}")
        time.sleep(0.3)
        q.task_done()

t1 = threading.Thread(target=producer)
t2 = threading.Thread(target=consumer)

t1.start()
t2.start()

t1.join()
q.put(None)  # 通知消费者退出
t2.join()

优点queue.Queue() 是线程安全的,内部已经做好了加锁处理,无需手动使用 Lock。


6. 守护线程(Daemon Thread)

守护线程会在主线程结束时自动退出。

python 复制代码
t = threading.Thread(target=worker)
t.daemon = True  # 设为守护线程
t.start()

适合做后台任务,例如日志记录或心跳检测。


7. 线程池(ThreadPoolExecutor)

如果需要频繁创建和销毁线程,可以使用线程池来提升性能。

python 复制代码
from concurrent.futures import ThreadPoolExecutor

def task(name):
    print(f"{name} 开始")
    return f"{name} 完成"

with ThreadPoolExecutor(max_workers=3) as executor:
    futures = [executor.submit(task, f"任务{i}") for i in range(5)]
    for future in futures:
        print(future.result())

8. 总结与建议

  • 计算密集型任务 :推荐使用多进程(multiprocessing)而不是多线程,避免 GIL 限制。
  • I/O 密集型任务 :推荐使用多线程或异步(asyncio)。
  • 使用 LockQueue 解决数据竞争和线程安全问题。
  • 合理规划线程数量,不要盲目创建过多线程。
  • 可以用 ThreadPoolExecutor 简化线程管理。
相关推荐
l1t23 分钟前
用wsl自带的python 3.10下载适用于3.12的pandas版本结合uv安装python 3.12模拟离线安装场景
python·pandas·uv
飞Link43 分钟前
【AI大模型实战】万字长文肝透大语言模型(LLM):从底层原理解析到企业级Python项目落地
开发语言·人工智能·python·语言模型·自然语言处理
翻斗包菜1 小时前
第 03 章 Python 操作 MySQL 数据库实战全解
数据库·python·mysql
xcjbqd01 小时前
如何修改Oracle服务器默认的日期格式_NLS_DATE_FORMAT全局配置
jvm·数据库·python
white-persist1 小时前
【vulhub spring CVE-2018-1270】CVE-2018-1270 Spring Messaging 远程命令执行漏洞 完整复现详细分析解释
java·服务器·网络·数据库·后端·python·spring
EnCi Zheng1 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
开发语言·python
潇洒畅想1 小时前
1.1 从∑到∫:用循环理解求和与累积
java·数据结构·python·算法
有一个好名字1 小时前
Claude Code 50+命令全解析
python
liliangcsdn1 小时前
LLM如何与mcp server交互示例
linux·开发语言·python
Lupino2 小时前
拯救迷失的荧光溶解氧传感器:从“三无”到“复活”的全记录
python