python如何使用多线程

Python (157) 2023-07-18 08:37:46

多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程(台湾译作“执行绪”),进而提升整体处理性能。

Python 多线程之使用方法

Python 提供多线程编程的模块有以下几个:

- _thread

- threading

- Queue

- multiprocessing

下面一一介绍:

1. _thread 模块提供了低级别的基本功能来支持多线程功能,提供简单的锁来确保同步,推荐使用 threading 模块。

2. threading 模块对 _thread 进行了封装,提供了更高级别,功能更强,更易于使用的线程管理的功能,对线程的支持更为完善,绝大多数情况下,只需要使用 threading 这个高级模块就够了。

相关推荐:《Python教程》

使用 threading 进行多线程操作:

方法一:是创建 threading.Thread 实例,调用其 start() 方法。

importtime
importthreading
deftask_thread(counter):
print(f'线程名称:{threading.current_thread().name}参数:{counter}开始时间:{time.strftime("%Y-%m-%d
%H:%M:%S")}')
num=counter
whilenum:
time.sleep(3)
num-=1
print(f'线程名称:{threading.current_thread().name}参数:{counter}结束时间:{time.strftime("%Y-%m-%d
%H:%M:%S")}')
if__name__=='__main__':
print(f'主线程开始时间:{time.strftime("%Y-%m-%d%H:%M:%S")}')
#初始化3个线程,传递不同的参数
t1=threading.Thread(target=task_thread,args=(3,))
t2=threading.Thread(target=task_thread,args=(2,))
t3=threading.Thread(target=task_thread,args=(1,))
#开启三个线程
t1.start()
t2.start()
t3.start()
#等待运行结束
t1.join()
t2.join()
t3.join()
print(f'主线程结束时间:{time.strftime("%Y-%m-%d%H:%M:%S")}')

运行结果如下所示:

主线程开始时间:2018-07-0623:03:46
线程名称:Thread-1参数:3开始时间:2018-07-0623:03:46
线程名称:Thread-2参数:2开始时间:2018-07-0623:03:46
线程名称:Thread-3参数:1开始时间:2018-07-0623:03:46
线程名称:Thread-3参数:1结束时间:2018-07-0623:03:49
线程名称:Thread-2参数:2结束时间:2018-07-0623:03:52
线程名称:Thread-1参数:3结束时间:2018-07-0623:03:55
主线程结束时间:2018-07-0623:03:55

方法二:继承 Thread 类,在子类中重写 run() 和 init() 方法。

importtime
importthreading
classMyThread(threading.Thread):
def__init__(self,counter):
super().__init__()
self.counter=counter
defrun(self):
print(
f'线程名称:{threading.current_thread().name}参数:{self.counter}开始时间:{time.strftime
("%Y-%m-%d%H:%M:%S")}'
)
counter=self.counter
whilecounter:
time.sleep(3)
counter-=1
print(
f'线程名称:{threading.current_thread().name}参数:{self.counter}结束时间:{time.strftime
("%Y-%m-%d%H:%M:%S")}'
)
if__name__=="__main__":
print(f'主线程开始时间:{time.strftime("%Y-%m-%d%H:%M:%S")}')
#初始化3个线程,传递不同的参数
t1=MyThread(3)
t2=MyThread(2)
t3=MyThread(1)
#开启三个线程
t1.start()
t2.start()
t3.start()
#等待运行结束
t1.join()
t2.join()
t3.join()
print(f'主线程结束时间:{time.strftime("%Y-%m-%d%H:%M:%S")}')

运行结果如下,与方法一的运行结果一致。

主线程开始时间:2018-07-0623:34:16
线程名称:Thread-1参数:3开始时间:2018-07-0623:34:16
线程名称:Thread-2参数:2开始时间:2018-07-0623:34:16
线程名称:Thread-3参数:1开始时间:2018-07-0623:34:16
线程名称:Thread-3参数:1结束时间:2018-07-0623:34:19
线程名称:Thread-2参数:2结束时间:2018-07-0623:34:22
线程名称:Thread-1参数:3结束时间:2018-07-0623:34:25
主线程结束时间:2018-07-0623:34:25

如果继承 Thread 类,想调用外部传入函数,代码如下所示:

importtime
importthreading
deftask_thread(counter):
print(f'线程名称:{threading.current_thread().name}参数:{counter}开始时间:{time.strftime("%Y-%m-%d
%H:%M:%S")}')
num=counter
whilenum:
time.sleep(3)
num-=1
print(f'线程名称:{threading.current_thread().name}参数:{counter}结束时间:{time.strftime("%Y-%m-%d
%H:%M:%S")}')
classMyThread(threading.Thread):
def__init__(self,target,args):
super().__init__()
self.target=target
self.args=args
defrun(self):
self.target(*self.args)
if__name__=="__main__":
print(f'主线程开始时间:{time.strftime("%Y-%m-%d%H:%M:%S")}')
#初始化3个线程,传递不同的参数
t1=MyThread(target=task_thread,args=(3,))
t2=MyThread(target=task_thread,args=(2,))
t3=MyThread(target=task_thread,args=(1,))
#开启三个线程
t1.start()
t2.start()
t3.start()
#等待运行结束
t1.join()
t2.join()
t3.join()
print(f'主线程结束时间:{time.strftime("%Y-%m-%d%H:%M:%S")}')

这样就和方法一是相通的,实例化自定义的线程类,运行结果不变。

THE END

发表回复