当前位置: 代码网 > it编程>前端脚本>Python > Python threading全景指南分享

Python threading全景指南分享

2025年08月07日 Python 我要评论
“并发不等于并行,但并发能让生活更美好。”——《python 并发编程实战》1. 为什么需要线程cpu 单核性能已逼近物理极限,要想让程序在相同时间内做

“并发不等于并行,但并发能让生活更美好。”——《python 并发编程实战》

1. 为什么需要线程

cpu 单核性能已逼近物理极限,要想让程序在相同时间内做更多事,必须“同时”做多件事。

  • 多进程 process:利用多核并行,资源隔离但开销大。
  • 协程 coroutine:单线程内切换,极致 i/o 友好,但无法利用多核。
  • 线程 thread:介于两者之间,共享内存、切换快,是 i/o 密集型任务的首选。

在 python 里,gil(global interpreter lock)限制了同一进程内只能有一条字节码在执行,进而“弱化”了线程在多核 cpu 上的并行能力。然而:

  • 线程在等待 i/o 时会主动释放 gil,因此下载、爬虫、聊天服务器等网络/磁盘 i/o 场景依旧收益巨大。
  • 对 cpu 密集型任务,可用 multiprocessing 或 c 扩展绕开 gil。

一句话:

当你想让程序“边读边写”“边收边发”“边阻塞边响应”,就用 threading。

2. 从 0 开始写线程

2.1 创建线程的两种姿势

import threading, time

# 方式一:把函数塞给 thread
def worker(n):
    print(f'worker {n} start')
    time.sleep(1)
    print(f'worker {n} done')

for i in range(3):
    t = threading.thread(target=worker, args=(i,))
    t.start()
# 方式二:继承 thread 并重写 run
class mythread(threading.thread):
    def __init__(self, n):
        super().__init__()
        self.n = n
    def run(self):
        print(f'mythread {self.n} start')
        time.sleep(1)
        print(f'mythread {self.n} done')

mythread(10).start()

2.2 join:别让主线程提前跑路

start() 只是告诉操作系统“可以调度了”,不保证立即执行。

threads = [threading.thread(target=worker, args=(i,)) for i in range(3)]
[t.start() for t in threads]
[t.join() for t in threads]  # 等全部结束
print('all done')

3. 线程同步:共享变量的“安全带”

3.1 lock(互斥锁)

竞争最激烈的原语,解决“读写交叉”问题。

counter = 0
lock = threading.lock()

def add():
    global counter
    for _ in range(100000):
        with lock:             # 等价于 lock.acquire(); try: ... finally: lock.release()
            counter += 1

threads = [threading.thread(target=add) for _ in range(2)]
[t.start() for t in threads]
[t.join() for t in threads]
print(counter)   # 200000

没有 lock 时,大概率得到 <200000 的错误结果。

3.2 rlock(可重入锁)

同一个线程可以多次 acquire,避免死锁。

rlock = threading.rlock()
def foo():
    with rlock:
        bar()

def bar():
    with rlock:   # 同一线程,再次获取成功
        pass

3.3 condition(条件变量)

经典“生产者-消费者”模型:

import random, time
q, max = [], 5
cond = threading.condition()

def producer():
    while true:
        with cond:
            while len(q) == max:
                cond.wait()          # 等待队列有空位
            item = random.randint(1, 100)
            q.append(item)
            print('+', item, q)
            cond.notify()            # 通知消费者
        time.sleep(0.5)

def consumer():
    while true:
        with cond:
            while not q:
                cond.wait()
            item = q.pop(0)
            print('-', item, q)
            cond.notify()
        time.sleep(0.6)

threading.thread(target=producer, daemon=true).start()
threading.thread(target=consumer, daemon=true).start()
time.sleep(5)

3.4 semaphore(信号量)

控制并发数量,例如“最多 3 个线程同时下载”。

sem = threading.semaphore(3)
def downloader(url):
    with sem:
        print('downloading', url)
        time.sleep(2)

3.5 event(事件)

线程间“发令枪”机制:

event = threading.event()

def waiter():
    print('wait...')
    event.wait()          # 阻塞
    print('go!')

threading.thread(target=waiter).start()
time.sleep(3)
event.set()               # 发令

3.6 barrier(栅栏)

n 个线程同时到达某点后再一起继续,适合分阶段任务。

barrier = threading.barrier(3)

def phase(name):
    print(name, 'ready')
    barrier.wait()
    print(name, 'go')

for i in range(3):
    threading.thread(target=phase, args=(i,)).start()

4. 线程局部变量:threadlocal

共享虽好,可有时我们想让每个线程拥有“私有副本”。

local = threading.local()

def show():
    print(f'{threading.current_thread().name} -> {local.x}')

def task(n):
    local.x = n
    show()

for i in range(3):
    threading.thread(target=task, args=(i,)).start()

5. 定时器 timer:延时任务

def hello():
    print('hello, timer')
threading.timer(3.0, hello).start()

常用于“超时取消”“心跳包”等场景。

6. 线程池:高并发下的“资源管家”

频繁创建/销毁线程代价高昂,python 3.2+ 内置 concurrent.futures.threadpoolexecutor 提供池化能力。

from concurrent.futures import threadpoolexecutor
import requests, time

urls = ['https://baidu.com'] * 20

def fetch(url):
    return requests.get(url).status_code

with threadpoolexecutor(max_workers=10) as pool:
    for code in pool.map(fetch, urls):
        print(code)
  • max_workers 默认为 min(32, os.cpu_count() + 4),i/o 密集场景可调高。
  • submit + as_completed 组合可实现“谁先完成谁处理”。

7. 调试与最佳实践

7.1 死锁排查

  • 保持加锁顺序一致。
  • 使用 try-lock + 超时。
  • 借助第三方库 deadlock-debug 或 faulthandler。

7.2 gil 与性能

  • cpu 密集:换多进程、cython、numpy、multiprocessing。
  • i/o 密集:放心用线程,瓶颈在网络延迟而非 gil。

7.3 守护线程 daemon

  • 当只剩守护线程时,程序直接退出。
  • 常用于后台心跳、日志写入,但不要做重要数据持久化。

7.4 日志线程名

logging.basicconfig(
    format='%(asctime)s [%(threadname)s] %(message)s',
    level=logging.info)

7.5 不要滥用

  • gui 程序:ui 线程勿阻塞,耗时操作放后台线程。
  • web 服务:wsgi 服务器(uwsgi、gunicorn)已帮你管理进程/线程,业务代码慎用线程。

8. 总结

维度线程进程协程
内存开销极低
数据共享难(需 ipc)
切换成本极低
适合场景i/o 密集cpu 密集超高并发 i/o
python 限制gil

使用 threading 的黄金法则:

  • 明确任务是 i/o 密集。
  • 共享变量就用锁,或者别共享。
  • 用 threadpoolexecutor 减少手工创建。
  • 守护线程只干辅助活。
  • 调试时给线程起名字、打日志。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com