线程的理解
每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
每个线程都有他自己的一组CPU寄存器,称为线程的上下文,该上下文反映了线程上次运行该线程的CPU寄存器的状态。
指令指针和堆栈指针寄存器是线程上下文中两个最重要的寄存器,线程总是在进程得到上下文中运行的,这些地址都用于标志拥有线程的进程地址空间中的内存。
线程可以被抢占(中断)。
在其他线程正在运行时,线程可以暂时搁置(也称为睡眠) -- 这就是线程的退让。
线程可以分为:
内核线程:由操作系统内核创建和撤销。
用户线程:不需要内核支持而在用户程序中实现的线程。
线程启动的两种方式
Python3 线程中常用的两个模块为:
(1) _thread, _thread.start_new_thread(function,args[, kwargs]) 方式
(2) threading(推荐使用), threading 实例化之后调用start()方法
函数式
调用 _thread 模块中的start_new_thread()函数来产生新线程。语法如下:
_thread.start_new_thread(function,args[, kwargs])
参数说明:
function - 线程函数。
args - 传递给线程函数的参数,他必须是个tuple类型。
kwargs - 可选参数。
import_thread, time
# 定义线程函数
defprint_time(threadName, delay):
count =0
whilecount <5:
time.sleep(delay)
count +=1
# 返回当前时间的时间戳(1970纪元后经过的浮点秒数), 并格式化输出
print("{}: {}".format(threadName, time.ctime(time.time()) ))
try:
_thread.start_new_thread( print_time, ("Thread-1",2))
_thread.start_new_thread( print_time, ("Thread-2",4))
except:
print("Error")
while1:
# 让线程有足够的时间完成
pass
E:\PyPro>pythonthread.py
Thread-1:ThuApr12 09:01:562018
Thread-2:ThuApr12 09:01:582018
Thread-1:ThuApr12 09:01:582018
Thread-1:ThuApr12 09:02:002018
Thread-2:ThuApr12 09:02:022018
Thread-1:ThuApr12 09:02:022018
Thread-1:ThuApr12 09:02:052018
Thread-2:ThuApr12 09:02:062018
Thread-2:ThuApr12 09:02:102018
Thread-2:ThuApr12 09:02:142018
类封装式
threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法:
threading.currentThread(): 返回当前的线程变量。
threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
threading.activeCount(): 返回正在运行的线程数量,与
len(threading.enumerate())有相同的结果。
线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:
run(): 用以表示线程活动的方法。
start():启动线程活动。
join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
isAlive(): 返回线程是否活动的。
getName(): 返回线程名。
setName(): 设置线程名。
import threading, time
# 创建进程类
classmyThread(threading.Thread):
# 构造函数
def__init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
# 重写run()
defrun(self):
print("Thread Strat:"+self.name)
print_time(self.name,self.counter,5)
print("Thread Exit:"+self.name)
defprint_time(threadName, delay, counter):
whilecounter:
time.sleep(delay)
print("{}: {}".format(threadName, time.ctime(time.time()) ))
counter -=1
# 创建线程
thread1 = myThread(1001,"Thread-1",1)
thread2 = myThread(1002,"Thread-2",2)
# 开启线程
print("Thread-1 is Alive? ", thread1.isAlive())
thread1.start()
thread2.start()
print("Thread-1 is Alive? ", thread1.isAlive())
thread1.join()
thread2.join()
print("Thread-1 is Alive? ", thread1.isAlive())
print("exit")
E:\PyPro>pythonthreadClass.py
Thread-1isAlive? False
ThreadStrat:Thread-1
ThreadStrat:Thread-2
Thread-1isAlive? True
Thread-1:ThuApr12 10:15:532018
Thread-1:ThuApr12 10:15:542018
Thread-2:ThuApr12 10:15:542018
Thread-1:ThuApr12 10:15:552018
Thread-1:ThuApr12 10:15:562018
Thread-2:ThuApr12 10:15:562018
Thread-1:ThuApr12 10:15:572018
ThreadExit:Thread-1
Thread-2:ThuApr12 10:15:582018
Thread-2:ThuApr12 10:16:002018
Thread-2:ThuApr12 10:16:022018
ThreadExit:Thread-2
Thread-1isAlive? False
exit
不难发现,线程是通过start()函数激活,而不是对象建立时激活的!
线程同步
多线程的优势在于可以同时运行多个任务(至少感觉起来是这样)。但是当线程需要共享数据时,可能存在数据不同步的问题。
使用 Thread 对象的 Lock 和 Rlock 可以实现简单的线程同步,这两个对象都有 acquire 方法和 release 方法,对于那些需要每次只允许一个线程操作的数据,可以将其操作放到 acquire 和 release 方法之间。
Lock 与RLock区别是:
rlock可以多次锁定,并acquire与release是成对出现的。
import threading, time
# 创建锁
threadLock = threading.Lock()
# 创建线程列表
threads = []
classmyThread(threading.Thread):
def__init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
defrun(self):
print("Thread Start: "+self.name)
# 获取锁,同步线程
threadLock.acquire()
print_time(self.name,self.counter,3)
# 释放锁,开启下一个线程
threadLock.release()
print("Thread Exit: "+self.name)
defprint_time(threadName, delay, counter):
whilecounter:
time.sleep(delay)
print("{}: {}".format(threadName, time.ctime()))
counter -=1
# 创建线程
thread1 = myThread(1001,"Thread-1",1)
thread2 = myThread(1002,"Thread-2",2)
# 开启线程
thread1.start()
thread2.start()
# 添加线程列表
threads.append(thread1)
threads.append(thread2)
# 等待所有线程完成
fortinthreads:
t.join()
print("exit")
E:\PyPro>pythonsynchronize.py
ThreadStart:Thread-1
ThreadStart:Thread-2
Thread-1:ThuApr12 11:00:492018
Thread-1:ThuApr12 11:00:502018
Thread-1:ThuApr12 11:00:512018
ThreadExit:Thread-1
Thread-2:ThuApr12 11:00:532018
Thread-2:ThuApr12 11:00:552018
Thread-2:ThuApr12 11:00:572018
ThreadExit:Thread-2
exit
线程优先级队列
Python 的 Queue 模块中提供了同步的、线程安全的队列类,包括FIFO队列Queue,LIFO队列LifoQueue,和优先级队列 PriorityQueue。
这些队列都实现了锁原语,能够在多线程中直接使用,可以使用队列来实现线程间的同步。
Queue 模块中的常用方法:
Queue.qsize() 返回队列的大小
Queue.empty() 如果队列为空,返回True,反之False
Queue.full() 如果队列满了,返回True,反之False
Queue.full 与 maxsize 大小对应
Queue.get([block[, timeout]])获取队列,timeout等待时间
Queue.get_nowait() 相当Queue.get(False)
Queue.put(item) 写入队列,timeout等待时间
Queue.put_nowait(item) 相当Queue.put(item, False)
Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
Queue.join() 实际上意味着等到队列为空,再执行别的操作
importqueue, threading, time
exitFlag =0
# 创建锁
queueLock = threading.Lock()
# 创建队列
workQueue = queue.Queue(10)
class myThread(threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print("Thread Start: "+ self.name)
process_data(self.name, self.q)
print("Thread Exit: "+ self.name)
def process_data(threadName, q):
whilenotexitFlag:
queueLock.acquire()
ifnotworkQueue.empty():
data = q.get()
queueLock.release()
print("{} processing {}".format(threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1","Thread-2","Thread-3"]
nameList = ["One","Two","Three","Four","Five"]
threads = []
threadID =1
# 创建新线程
fort Name in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID +=1
# 填充队列
queueLock.acquire()
print("队列填充中>>>>>>>>>>>>>>")
time.sleep(1)
forwordinnameList:
workQueue.put(word)
print("队列填充完毕>>>>>>>>>>>>>>")
queueLock.release()
# 等待队列清空
whilenotworkQueue.empty():
pass
# 通知线程退出
exitFlag =1
# 等待所有线程完成
fortinthreads:
t.join()
print("exit")
E:\PyPro>pythonqueueue.py
Thread Start:Thread-1
Thread Start:Thread-2
Thread Start:Thread-3
队列填充中>>>>>>>>>>>>>>
队列填充完毕>>>>>>>>>>>>>>
Thread-3processing One
Thread-1processing Two
Thread-2processing Three
Thread-3processing Four
Thread-1processing Five
Thread Exit:Thread-2
Thread Exit:Thread-1
Thread Exit:Thread-3
exit
源码中其实实现了三个进程读取同一个队列,按照先进先出原则实现锁定。
用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。
join的作用是保证当前线程执行完成后,再执行其它线程。join可以有timeout参数,表示阻塞其它线程timeout秒后,不再阻塞。。一般线程的start()之后,所有操作结束后都要进行thread.join()。确保语句的输出是join()后面的程序是等线程结束后再执行的。