Python中的多线程实例(简单易懂)

作者:时代&信念 时间:2021-12-07 04:09:47 

前言:

多线程简单理解就是:一个CPU,也就是单核,将时间切成一片一片的,CPU轮转着去处理一件一件的事情,到了规定的时间片就处理下一件事情。

1.python中显示当前线程信息的属性和方法

# coding:utf-8
# 导入threading包
import threading
if __name__ == "__main__":
   print("当前活跃线程的数量", threading.active_count())
   print("将当前所有线程的具体信息展示出来", threading.enumerate())
   print("当前的线程的信息展示", threading.current_thread())

效果图:

Python中的多线程实例(简单易懂)

2.添加一个线程

# coding:utf-8
import threading
import time
def job1():
   # 让这个线程多执行几秒
   time.sleep(5)
   print("the number of T1 is %s" % threading.current_thread())
if __name__ == "__main__":
   # 创建一个新的线程
   new_thread = threading.Thread(target=job1, name="T1")
   # 启动新线程
   new_thread.start()
   print("当前线程数量为", threading.active_count())
   print("所有线程的具体信息", threading.enumerate())
   print("当前线程具体信息", threading.current_thread())

效果图:

Python中的多线程实例(简单易懂)

3.线程中的join函数

(1)预想的是,执行完线程1,然后输出All done…“理想很丰满,现实却不是这样的”

# coding:utf-8
import threading
import time
def job1():
   print("T1 start")
   for i in range(5):
       time.sleep(1)
       print(i)
   print("T1 finish")
def main():
   # 新创建一个线程
   new_thread = threading.Thread(target=job1, name="T1")
   # 启动新线程
   new_thread.start()
   print("All done...")
if __name__ == "__main__":
   main()

效果图:

Python中的多线程实例(简单易懂)

(2)为了达到我们的预期,我们使用join函数,将T1线程进行阻塞。join函数进行阻塞是什么意思?就是哪个线程使用了join函数,当这个线程正在执行时,在他之后的线程程序不能执行,得等这个被阻塞的线程全部执行完毕之后,方可执行!

# coding:utf-8
import threading
import time
def job1():
   print("T1 start")
   for i in range(5):
       time.sleep(1)
       print(i)
   print("T1 finish")
def main():
   # 新创建一个线程
   new_thread = threading.Thread(target=job1, name="T1")
   # 启动新线程
   new_thread.start()
   # 阻塞这个T1线程
   new_thread.join()
   print("All done...")
if __name__ == "__main__":
   main()

效果图:

Python中的多线程实例(简单易懂)

4.使用Queue存储线程的结果

线程的执行结果,无法通过return进行返回,使用Queue存储。

# coding:utf-8
import threading
from queue import Queue
"""
   Queue的使用
"""
def job(l, q):
   for i in range(len(l)):
       l[i] = l[i] ** 2
   q.put(l)
def multithreading():
   # 创建队列
   q = Queue()
   # 线程列表
   threads = []
   # 二维列表
   data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [6, 6, 6]]
   for i in range(4):
       t = threading.Thread(target=job, args=(data[i], q))
       t.start()
       threads.append(t)

# 对所有线程进行阻塞
   for thread in threads:
       thread.join()
   results = []
   # 将新队列中的每个元素挨个放到结果列表中
   for _ in range(4):
       results.append(q.get())
   print(results)
if __name__ == "__main__":
   multithreading()

效果图:

Python中的多线程实例(简单易懂)

5.线程锁lock

当同时启动多个线程时,各个线程之间会互相抢占计算资源,会造成程序混乱。

举个栗子:

当我们在选课系统选课时,当前篮球课还有2个名额,我们三个人去选课。

选课顺序为stu1 stu2 stu3,应该依次打印他们三个的选课过程,但是现实情况却是:

# coding:utf-8
import threading
import time

def stu1():
   print("stu1开始选课")
   global course
   if course > 0:
       course -= 1
       time.sleep(2)
       print("stu1选课成功,现在篮球课所剩名额为%d" % course)
   else:
       time.sleep(2)
       print("stu1选课失败,篮球课名额为0,请选择其他课程")
def stu2():
   print("stu2开始选课")
   global course
   if course > 0:
       course -= 1
       time.sleep(2)
       print("stu2选课成功,现在篮球课所剩名额为%d" % course)
   else:
       time.sleep(2)
       print("stu2选课失败,篮球课名额为0,请选择其他课程")

def stu3():
   print("stu3开始选课")
   global course
   if course > 0:
       course -= 1
       time.sleep(2)
       print("stu3选课成功")
       print("篮球课所剩名额为%d" %course)
   else:
       time.sleep(2)
       print("stu3选课失败,篮球课名额为0,请选择其他课程")
if __name__ == "__main__":
   # 篮球课名额
   course = 2
   T1 = threading.Thread(target=stu1, name="T1")
   T2 = threading.Thread(target=stu2, name="T2")
   T3 = threading.Thread(target=stu3, name="T3")
   T1.start()
   T2.start()
   T3.start()

效果图:

Python中的多线程实例(简单易懂)

为了解决这种情况,我们使用lock线程同步锁,在线程并发执行时,保证每个线程执行的原子性。有效防止了共享统一数据时,线程并发执行的混乱。

改进的代码如下:

# coding:utf-8
import threading
import time
def stu1():
   global lock
   lock.acquire()
   print("stu1开始选课")
   global course
   if course > 0:
       course -= 1
       time.sleep(2)
       print("stu1选课成功,现在篮球课所剩名额为%d" % course)
   else:
       time.sleep(2)
       print("stu1选课失败,篮球课名额为0,请选择其他课程")
   lock.release()

def stu2():
   global lock
   lock.acquire()
   print("stu2开始选课")
   global course
   if course > 0:
       course -= 1
       print("stu2选课成功,现在篮球课所剩名额为%d" % course)
   else:
       time.sleep(1)
       print("stu2选课失败,篮球课名额为0,请选择其他课程")
   lock.release()

def stu3():
   global lock
   lock.acquire()
   print("stu3开始选课")
   global course
   if course > 0:
       course -= 1
       time.sleep(1)
       print("stu3选课成功,现在篮球课所剩名额为%d" % course)
   else:
       time.sleep(1)
       print("stu3选课失败,篮球课名额为0,请选择其他课程")
   lock.release()

if __name__ == "__main__":
   # 篮球课名额
   course = 2
   # 创建同步锁
   lock = threading.Lock()
   T1 = threading.Thread(target=stu1, name="T1")
   T2 = threading.Thread(target=stu2, name="T2")
   T3 = threading.Thread(target=stu3, name="T3")
   T1.start()
   T2.start()
   T3.start()

效果图:

Python中的多线程实例(简单易懂)

来源:https://blog.csdn.net/Elon15/article/details/125350491

标签:Python,多线程
0
投稿

猜你喜欢

  • 用python3 urllib破解有道翻译反爬虫机制详解

    2022-05-27 00:23:07
  • 在SQL Server中使用索引的技巧

    2009-02-24 17:50:00
  • 最具创意的广告牌全集

    2007-09-21 19:54:00
  • Oracle 数据库 临时数据的处理方法

    2009-07-02 11:48:00
  • 用Python手把手教你实现2048小游戏

    2023-02-22 23:27:57
  • PHP7正式版测试,性能惊艳!

    2023-09-12 07:41:43
  • Oracle SQL性能优化系列学习一

    2010-07-26 13:14:00
  • javascript表单验证和Window详解

    2023-07-14 02:14:38
  • Python自定义一个类实现字典dict功能的方法

    2023-07-20 22:45:59
  • JS重现80后儿时经典拼板(模板)游戏

    2011-09-11 18:36:46
  • Oracle 数据 使用游标

    2009-07-02 12:14:00
  • Oracle AS关键字 提示错误

    2011-04-18 12:42:00
  • 详解python 爬取12306验证码

    2022-07-17 20:38:20
  • 用python登录带弱图片验证码的网站

    2023-04-28 12:22:22
  • 简单了解python列表和元组的区别

    2022-02-11 17:14:43
  • 学习CSS布局心得

    2007-05-11 16:50:00
  • django restframework serializer 增加自定义字段操作

    2023-12-17 20:36:10
  • python执行scp命令拷贝文件及文件夹到远程主机的目录方法

    2023-07-10 09:12:19
  • 浏览器的字体等宽空格

    2008-08-28 12:25:00
  • 解决SpringBoot启动过后不能访问jsp页面的问题(超详细)

    2023-06-13 19:43:31
  • asp之家 网络编程 m.aspxhome.com