Python3 线程中常用的两个多线程模块:
- _thread(已经弃用)
- threading(推荐使用)
1、Threading模块
threading 模块常用方法:
- threading.currentThread(): 返回当前的线程变量。
- threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
- threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
线程模块的Thread类来处理线程方法:
run(): 用以表示线程活动的方法。
start():启动线程活动。
join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
isAlive(): 返回线程是否活动的。
getName(): 返回线程名。
setName(): 设置线程名。
1.1、threading.Thread创建线程
import threading
import time
originalNumber=10
def addStepA(varNum):
global originalNumber
while(varNum>0):
originalNumber+=1
time.sleep(1)
varNum=varNum-1
print(threading.current_thread().name,"-->:",originalNumber)
def addStepB(varNum):
global originalNumber
while(varNum>0):
originalNumber+=1
time.sleep(1)
varNum=varNum-1
print(threading.current_thread().name,"-->:",originalNumber)
thread_A=threading.Thread(target=addStepA,args=(10,))
thread_B=threading.Thread(target=addStepB,args=(10,))
thread_A.start()
thread_B.start()
thread_A.join()
thread_B.join()
print("所有子线程运行结束")
1.2、子类继承Threing.thread
import threading
import time
def callOperate(loopNum):
while(loopNum>0):
print(threading.currentThread().name,"-->:",loopNum)
time.sleep(1)
loopNum-=1
class ParallelThread(threading.Thread):
def __init__(self,initNum):
super().__init__()
self.initNum=initNum
def run(self):
callOperate(self.initNum)
readThread_A=ParallelThread(5)
readThread_B=ParallelThread(10)
readThread_A.start()
readThread_B.start()
readThread_A.join()
readThread_B.join()
print("子线程全部运行结束")
Thread-1 -->: 5
Thread-2 -->: 10
Thread-2 -->: 9
Thread-1 -->: 4
Thread-2 -->: 8
Thread-1 -->: 3
Thread-2 -->: 7
Thread-1 -->: 2
Thread-2 -->: 6
Thread-1 -->: 1
Thread-2 -->: 5
Thread-2 -->: 4
Thread-2 -->: 3
Thread-2 -->: 2
Thread-2 -->: 1
子线程全部运行结束
1.3、共享全局变量
1.3.1、threading.Thread创建线程
import threading
import time
originalNumber=20
lock=threading.Lock()
def addStepA(varNum):
global originalNumber
while(varNum>0):
lock.acquire()
originalNumber+=1
print(threading.current_thread().name,"-->:",originalNumber)
lock.release()
varNum=varNum-1
time.sleep(1.5)
def addStepB(varNum):
global originalNumber
while(varNum>0):
lock.acquire()
originalNumber+=1
print(threading.current_thread().name,"-->:",originalNumber)
lock.release()
varNum=varNum-1
time.sleep(1)
thread_A=threading.Thread(target=addStepA,args=(10,))
thread_B=threading.Thread(target=addStepB,args=(10,))
thread_A.start()
thread_B.start()
thread_A.join()
thread_B.join()
print("所有子线程运行结束")
Thread-1 -->: 21
Thread-2 -->: 22
Thread-2 -->: 23
Thread-1 -->: 24
Thread-2 -->: 25
Thread-1 -->: 26
Thread-2 -->: 27
Thread-2 -->: 28
Thread-1 -->: 29
Thread-2 -->: 30
Thread-1 -->: 31
Thread-2 -->: 32
Thread-2 -->: 33
Thread-1 -->: 34
Thread-2 -->: 35
Thread-1 -->: 36
Thread-2 -->: 37
Thread-1 -->: 38
Thread-1 -->: 39
Thread-1 -->: 40
所有子线程运行结束
1.3.2、子类继承Thread.thread类
import threading
import time
class ParallelThread(threading.Thread):
initNum=20
lock=threading.Lock()
def __init__(self):
super().__init__()
def run(self):
while(self.initNum):
self.lock.acquire()
print(threading.currentThread().name,"-->:",self.initNum)
self.initNum=self.initNum - 1
self.lock.release()
time.sleep(1)
readThread_A=ParallelThread()
readThread_B=ParallelThread()
readThread_A.start()
readThread_B.start()
readThread_A.join()
readThread_B.join()
print("子线程全部运行结束")
说明:次数代码未修改完成,未实现多线程共享全局变量
|