使用Python threading模块创建多线程程序

本套课在线学习视频(网盘地址,保存到网盘即可免费观看):

​https://pan.quark.cn/s/4999d631ac47​

本文将详细介绍如何在Python中利用threading模块创建和管理多线程程序,并通过示例代码演示如何定义和启动线程,以及如何实现线程间的同步。

00:00 - 使用Python threading模块创建多线程程序

定义和启动线程

首先,我们学习了如何定义一个Thread对象,并通过调用其start()方法来启动一个新的线程,同时指定了目标函数及其所需的参数。

import threading
import time

def task(task_id):
    print(f"Task {task_id} started")
    time.sleep(2)
    print(f"Task {task_id} completed")

# 创建线程
thread = threading.Thread(target=task, args=(1,))

# 启动线程
thread.start()

# 等待线程完成
thread.join()

单线程与多线程执行顺序和时间差异

通过示例说明了单线程与多线程执行顺序和时间差异。

import threading
import time

def task(task_id):
    print(f"Task {task_id} started")
    time.sleep(2)
    print(f"Task {task_id} completed")

# 单线程执行
start_time = time.time()
task(1)
task(2)
print(f"Single thread total time: {time.time() - start_time}")

# 多线程执行
start_time = time.time()
threads = []
for i in range(1, 3):
    thread = threading.Thread(target=task, args=(i,))
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

print(f"Multi-thread total time: {time.time() - start_time}")

04:44 - 实现多线程程序:创建、启动与同步

传递参数、启动线程以及使用join()方法实现线程间的同步

重点讲解了如何传递参数、启动线程以及使用join()方法实现线程间的同步,以确保主线程在所有子线程完成后才继续执行,从而正确计算总耗时。

import threading
import time

def task(task_id, duration):
    print(f"Task {task_id} started")
    time.sleep(duration)
    print(f"Task {task_id} completed")

# 创建并启动多个线程
threads = []
for i in range(1, 4):
    thread = threading.Thread(target=task, args=(i, i))
    threads.append(thread)
    thread.start()

# 使用join()方法同步线程
for thread in threads:
    thread.join()

print("All tasks completed")

09:26 - 使用for循环实现多线程

通过在for循环中创建并启动多个线程

通过在for循环中创建并启动多个线程,将它们添加到一个任务列表中,并使用join方法确保所有线程完成后继续执行下一条指令,实现了多线程的同时运行但总执行时间不变的效果。

import threading
import time

def task(task_id):
    print(f"Task {task_id} started")
    time.sleep(2)
    print(f"Task {task_id} completed")

# 创建并启动多个线程
threads = []
for i in range(1, 6):
    thread = threading.Thread(target=task, args=(i,))
    threads.append(thread)
    thread.start()

# 使用join()方法同步线程
for thread in threads:
    thread.join()

print("All tasks completed")

通过这些示例代码,您可以更好地理解如何在Python中使用threading模块创建和管理多线程程序,以及如何实现线程间的同步,从而提高程序的执行效率。

相关推荐

  1. 使用Python threading模块创建线程序

    2024-07-10 11:40:01       10 阅读
  2. c# 线创建线同步

    2024-07-10 11:40:01       25 阅读
  3. C++ 线笔记1 线创建

    2024-07-10 11:40:01       16 阅读
  4. C# 创建线的函数

    2024-07-10 11:40:01       42 阅读
  5. 线使用

    2024-07-10 11:40:01       44 阅读
  6. 线常见使用

    2024-07-10 11:40:01       14 阅读
  7. Python----线使用

    2024-07-10 11:40:01       13 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 11:40:01       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 11:40:01       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 11:40:01       4 阅读
  4. Python语言-面向对象

    2024-07-10 11:40:01       4 阅读

热门阅读

  1. 探索数据的奥秘:sklearn中的聚类分析技术

    2024-07-10 11:40:01       8 阅读
  2. FPGA之术语

    2024-07-10 11:40:01       7 阅读
  3. 【Axure视频教程】页面滚动距离函数

    2024-07-10 11:40:01       5 阅读
  4. 如何判断服务器是否被攻击

    2024-07-10 11:40:01       10 阅读
  5. 网络服务器都包含哪些方面需要维护?

    2024-07-10 11:40:01       8 阅读
  6. 定制化正则化:在Mojo模型中动态应用自定义方法

    2024-07-10 11:40:01       11 阅读
  7. 稀疏之美:在Mojo模型中实现特征的稀疏表示

    2024-07-10 11:40:01       11 阅读
  8. AI开发者的编程语言Mojo:入门指南

    2024-07-10 11:40:01       11 阅读