Python并发编程:多线程(threading模块)

简介: 本文详细介绍了Python的threading模块,包括线程的创建、线程同步、线程池的使用,并通过多个示例展示了如何在实际项目中应用这些技术。通过学习这些内容,您应该能够熟练掌握Python中的多线程编程,提高编写并发程序的能力。多线程编程可以显著提高程序的并发性能,但也带来了新的挑战和问题。在使用多线程时,需要注意避免死锁、限制共享资源的访问,并尽量使用线程池来管理和控制线程。

Python是一门强大的编程语言,提供了多种并发编程方式,其中多线程是非常重要的一种。本文将详细介绍Python的threading模块,包括其基本用法、线程同步、线程池等,最后附上一个综合详细的例子并输出运行结果。

1. 多线程概述

多线程是一种并发编程方式,它允许在一个进程内同时运行多个线程,从而提高程序的运行效率。线程是轻量级的进程,拥有自己的栈空间,但共享同一个进程的内存空间。

2. threading模块

threading模块是Python标准库中的一个模块,提供了创建和管理线程的工具。

2.1 创建线程

可以通过继承threading.Thread类或者直接使用threading.Thread创建线程。

示例:继承threading.Thread

import threading

class MyThread(threading.Thread):
    def run(self):
        for i in range(5):
            print(f'Thread {self.name} is running')

if __name__ == "__main__":
    threads = [MyThread() for _ in range(3)]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()

示例:直接使用threading.Thread

import threading

def thread_function(name):
    for i in range(5):
        print(f'Thread {name} is running')

if __name__ == "__main__":
    threads = [threading.Thread(target=thread_function, args=(i,)) for i in range(3)]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()

2.2 线程同步

在多线程编程中,经常需要确保多个线程在访问共享资源时不发生冲突。这时需要用到线程同步工具,如锁(Lock)、条件变量(Condition)、信号量(Semaphore)等。

示例:使用锁(Lock)

import threading

counter = 0
lock = threading.Lock()

def increment_counter():
    global counter
    for _ in range(1000):
        with lock:
            counter += 1

if __name__ == "__main__":
    threads = [threading.Thread(target=increment_counter) for _ in range(5)]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()
    print(f'Final counter value: {counter}')

2.3 线程池

Python的concurrent.futures模块提供了线程池,可以更方便地管理和控制线程。

示例:使用线程池

from concurrent.futures import ThreadPoolExecutor

def task(name):
    for i in range(5):
        print(f'Task {name} is running')

if __name__ == "__main__":
    with ThreadPoolExecutor(max_workers=3) as executor:
        futures = [executor.submit(task, i) for i in range(3)]
        for future in futures:
            future.result()

3. 综合详细的例子

下面是一个综合详细的例子,模拟一个简单的爬虫程序,使用多线程来提高爬取效率,并使用线程同步工具来保证数据的一致性。

import threading
import requests
from queue import Queue
from bs4 import BeautifulSoup

class WebCrawler:
    def __init__(self, base_url, num_threads):
        self.base_url = base_url
        self.num_threads = num_threads
        self.urls_to_crawl = Queue()
        self.crawled_urls = set()
        self.data_lock = threading.Lock()

    def crawl_page(self, url):
        try:
            response = requests.get(url)
            soup = BeautifulSoup(response.text, 'html.parser')
            links = soup.find_all('a', href=True)
            with self.data_lock:
                for link in links:
                    full_url = self.base_url + link['href']
                    if full_url not in self.crawled_urls:
                        self.urls_to_crawl.put(full_url)
                self.crawled_urls.add(url)
            print(f'Crawled: {url}')
        except Exception as e:
            print(f'Failed to crawl {url}: {e}')

    def worker(self):
        while not self.urls_to_crawl.empty():
            url = self.urls_to_crawl.get()
            if url not in self.crawled_urls:
                self.crawl_page(url)
            self.urls_to_crawl.task_done()

    def start_crawling(self, start_url):
        self.urls_to_crawl.put(start_url)
        threads = [threading.Thread(target=self.worker) for _ in range(self.num_threads)]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()

if __name__ == "__main__":
    crawler = WebCrawler(base_url='https://example.com', num_threads=5)
    crawler.start_crawling('https://example.com')

运行结果

Crawled: https://example.com
Crawled: https://example.com/about
Crawled: https://example.com/contact
...

4. 多线程编程注意事项

虽然多线程编程可以显著提高程序的并发性能,但它也带来了新的挑战和问题。在使用多线程时,需要注意以下几点:

4.1 避免死锁

死锁是指两个或多个线程相互等待对方释放资源,从而导致程序无法继续执行的情况。避免死锁的一种方法是尽量减少线程持有锁的时间,或者通过加锁的顺序来避免循环等待。

示例:避免死锁

import threading

lock1 = threading.Lock()
lock2 = threading.Lock()

def thread1():
    with lock1:
        print("Thread 1 acquired lock1")
        with lock2:
            print("Thread 1 acquired lock2")

def thread2():
    with lock2:
        print("Thread 2 acquired lock2")
        with lock1:
            print("Thread 2 acquired lock1")

if __name__ == "__main__":
    t1 = threading.Thread(target=thread1)
    t2 = threading.Thread(target=thread2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()

4.2 限制共享资源的访问

在多线程编程中,避免多个线程同时访问共享资源是非常重要的。可以使用线程同步工具,如锁(Lock)、条件变量(Condition)等,来限制对共享资源的访问。

示例:使用条件变量

import threading

condition = threading.Condition()
items = []

def producer():
    global items
    for i in range(5):
        with condition:
            items.append(i)
            print(f"Produced {i}")
            condition.notify()

def consumer():
    global items
    while True:
        with condition:
            while not items:
                condition.wait()
            item = items.pop(0)
            print(f"Consumed {item}")

if __name__ == "__main__":
    t1 = threading.Thread(target=producer)
    t2 = threading.Thread(target=consumer)
    t1.start()
    t2.start()
    t1.join()
    t2.join()

4.3 使用线程池

线程池可以帮助我们更方便地管理和控制线程,避免频繁创建和销毁线程带来的开销。Python的concurrent.futures模块提供了一个简单易用的线程池接口。

示例:使用线程池

from concurrent.futures import ThreadPoolExecutor

def task(name):
    print(f'Task {name} is running')

if __name__ == "__main__":
    with ThreadPoolExecutor(max_workers=3) as executor:
        futures = [executor.submit(task, i) for i in range(3)]
        for future in futures:
            future.result()

5. 综合详细的例子

下面是一个综合详细的例子,模拟一个多线程的文件下载器,使用线程池来管理多个下载线程,并确保文件下载的完整性。

文件下载器示例

import threading
import requests
from concurrent.futures import ThreadPoolExecutor

class FileDownloader:
    def __init__(self, urls, num_threads):
        self.urls = urls
        self.num_threads = num_threads
        self.download_lock = threading.Lock()
        self.downloaded_files = []

    def download_file(self, url):
        try:
            response = requests.get(url)
            filename = url.split('/')[-1]
            with self.download_lock:
                with open(filename, 'wb') as f:
                    f.write(response.content)
                self.downloaded_files.append(filename)
            print(f'Downloaded: {filename}')
        except Exception as e:
            print(f'Failed to download {url}: {e}')

    def start_downloading(self):
        with ThreadPoolExecutor(max_workers=self.num_threads) as executor:
            executor.map(self.download_file, self.urls)

if __name__ == "__main__":
    urls = [
        'https://example.com/file1.txt',
        'https://example.com/file2.txt',
        'https://example.com/file3.txt'
    ]
    downloader = FileDownloader(urls, num_threads=3)
    downloader.start_downloading()
    print("Downloaded files:", downloader.downloaded_files)

运行结果

Downloaded: file1.txt
Downloaded: file2.txt
Downloaded: file3.txt
Downloaded files: ['file1.txt', 'file2.txt', 'file3.txt']

6. 总结

本文详细介绍了Python的threading模块,包括线程的创建、线程同步、线程池的使用,并通过多个示例展示了如何在实际项目中应用这些技术。通过学习这些内容,您应该能够熟练掌握Python中的多线程编程,提高编写并发程序的能力。


多线程编程可以显著提高程序的并发性能,但也带来了新的挑战和问题。在使用多线程时,需要注意避免死锁、限制共享资源的访问,并尽量使用线程池来管理和控制线程。

作者:Rjdeng

链接:https://juejin.cn/post/7397285315822862373

相关文章
|
3月前
|
数据采集 存储 JSON
Python爬取知乎评论:多线程与异步爬虫的性能优化
Python爬取知乎评论:多线程与异步爬虫的性能优化
|
3月前
|
人工智能 安全 调度
Python并发编程之线程同步详解
并发编程在Python中至关重要,线程同步确保多线程程序正确运行。本文详解线程同步机制,包括互斥锁、信号量、事件、条件变量和队列,探讨全局解释器锁(GIL)的影响及解决线程同步问题的最佳实践,如避免全局变量、使用线程安全数据结构、精细化锁的使用等。通过示例代码帮助开发者理解并提升多线程程序的性能与可靠性。
114 0
|
14天前
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
53 4
|
8天前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
87 0
|
2月前
|
存储 安全 数据处理
Python 内置模块 collections 详解
`collections` 是 Python 内置模块,提供多种高效数据类型,如 `namedtuple`、`deque`、`Counter` 等,帮助开发者优化数据处理流程,提升代码可读性与性能,适用于复杂数据结构管理与高效操作场景。
103 0
|
3月前
|
数据采集 监控 调度
干货分享“用 多线程 爬取数据”:单线程 + 协程的效率反超 3 倍,这才是 Python 异步的正确打开方式
在 Python 爬虫中,多线程因 GIL 和切换开销效率低下,而协程通过用户态调度实现高并发,大幅提升爬取效率。本文详解协程原理、实战对比多线程性能,并提供最佳实践,助你掌握异步爬虫核心技术。
|
3月前
|
数据安全/隐私保护 Python
抖音私信脚本app,协议私信群发工具,抖音python私信模块
这个实现包含三个主要模块:抖音私信核心功能类、辅助工具类和主程序入口。核心功能包括登录
|
4月前
|
JSON 算法 Java
打造终端里的下载利器:Python实现可恢复式多线程下载器
在数字时代,大文件下载已成为日常需求。本文教你用Python打造专业级下载器,支持断点续传、多线程加速、速度限制等功能,显著提升终端下载体验。内容涵盖智能续传、多线程分块下载、限速控制及Rich库构建现代终端界面,助你从零构建高效下载工具。
245 1
|
3月前
|
数据采集 存储 Java
多线程Python爬虫:加速大规模学术文献采集
多线程Python爬虫:加速大规模学术文献采集
|
5月前
|
机器学习/深度学习 消息中间件 存储
【高薪程序员必看】万字长文拆解Java并发编程!(9-2):并发工具-线程池
🌟 ​大家好,我是摘星!​ 🌟今天为大家带来的是并发编程中的强力并发工具-线程池,废话不多说让我们直接开始。
190 0

推荐镜像

更多