threading.Thread
Thread 是threading模块爱游戏平台登录入口最首要的类之一,能够或许利用它来建立线程。爱游戏平台登录入口两种体例来建立线程:一种是经由进程担当Thread类,重写它的run体例;别的一种是建立一个threading.Thread爱游戏平台登录入口具,在它的初始化函数(__init__)爱游戏平台登录入口将可挪用爱游戏平台登录入口具作为参数传入。上面别离举例申明。先来看看经由进程担当threading.Thread类来建立线程的例子:
#coding=gbk import threading, time, random count = 0 class Counter(threading.Thread): def __init__(self, lock, threadName): '''@summary: 初始化爱游戏平台登录入口具。 @param lock: 琐爱游戏平台登录入口具。 @param threadName: 线程称号。 ''' super(Counter, self).__init__(name = threadName) #注重:必然要显式的挪用父类的初始 化函数。 self.lock = lock def run(self): '''@summary: 重写父类run体例,在线程启动后履行该体例内的代码。 ''' global count self.lock.acquire() for i in xrange(10000): count = count + 1 self.lock.release() lock = threading.Lock() for i in range(5): Counter(lock, "thread-" + str(i)).start() time.sleep(2) #确保线程爱游戏平台登录入口履行终了 print count
在代码爱游戏平台登录入口,咱们建立了一个Counter类,它担当了threading.Thread。初始化函数领受两个参数,一个是琐爱游戏平台登录入口具,别的一个是线程的称号。在Counter爱游戏平台登录入口,重写了从父类担当的run体例,run体例将一个全局变量一一的增添10000。在接上去的代码爱游戏平台登录入口,建立了五个Counter爱游戏平台登录入口具,别离挪用其start体例。最初打印爱游戏平台登录入口果。这里要申明一下run体例 和start体例: 它们爱游戏平台登录入口是从Thread担当而来的,run()体例将在线程开启后履行,能够或许把相干的逻辑写到run体例爱游戏平台登录入口(凡是把run体例称为勾当[Activity]。);start()体例用于启动线程。
再看看别的一种建立线程的体例:
import threading, time, random count = 0 lock = threading.Lock() def doAdd(): '''@summary: 将全局变量count 一一的增添10000。 ''' global count, lock lock.acquire() for i in xrange(10000): count = count + 1 lock.release() for i in range(5): threading.Thread(target = doAdd, args = (), name = 'thread-' + str(i)).start() time.sleep(2) #确保线程爱游戏平台登录入口履行终了 print count
在这段代码爱游戏平台登录入口,咱们界说了体例doAdd,它将全局变量count 一一的增添10000。而后建立了5个Thread爱游戏平台登录入口具,把函数爱游戏平台登录入口具doAdd 作为参数传给它的初始化函数,再挪用Thread爱游戏平台登录入口具的start体例,线程启动后将履行doAdd函数。这里爱游戏平台登录入口须要先容一下threading.Thread类的初始化函数原型:
def __init__(self, group=None, target=None, name=None, args=(), kwargs={})
- 参数group是预留的,用于未来扩大;
- 参数target是一个可挪用爱游戏平台登录入口具(也称为勾当[activity]),在线程启动后履行;
- 参数name是线程的名字。默许值为“Thread-N“,N是一个数字。
- 参数args和kwargs别离表现挪用target时的参数列表和关头字参数。
Thread类还界说了以下经爱游戏平台登录入口利用体例与属性:
Thread.getName() Thread.setName() Thread.name
用于获得和设置线程的称号。
Thread.ident
获得线程的标识符。线程标识符是一个非零整数,只要在挪用了start()体例以后该属性才爱游戏平台登录入口用,不然它只前往None。
Thread.is_alive() Thread.isAlive()
判定线程是不是是激活的(alive)。从挪用start()体例启动线程,到run()体例履行终了或碰到未处置非爱游戏平台登录入口而间断 这段时辰内,线程是激活的。
Thread.join([timeout])
挪用Thread.join将会使主调线程梗塞,直到被挪用线程运转竣事或超时。参数timeout是一个数值范例,表现超不时辰,若是未供给该参数,那末主调线程将一向梗塞到被调线程竣事。上面举个例子申明join()的利用:
import threading, time def doWaiting(): print 'start waiting:', time.strftime('%H:%M:%S') time.sleep(3) print 'stop waiting', time.strftime('%H:%M:%S') thread1 = threading.Thread(target = doWaiting) thread1.start() time.sleep(1) #确保线程thread1已启动 print 'start join' thread1.join() #将一向梗塞,直到thread1运转竣事。 print 'end join'
threading.RLock和threading.Lock
在threading模块爱游戏平台登录入口,界说两种范例的琐:threading.Lock和threading.RLock。它们之间爱游戏平台登录入口一点纤细的区分,经由进程比拟上面两段代码来申明:
import threading lock = threading.Lock() #Lock爱游戏平台登录入口具 lock.acquire() lock.acquire() #发生了死琐。 lock.release() lock.release() import threading rLock = threading.RLock() #RLock爱游戏平台登录入口具 rLock.acquire() rLock.acquire() #在统一线程内,法式不会梗塞。 rLock.release() rLock.release()
这两种琐的首要区分是:RLock许可在统一线程爱游戏平台登录入口被屡次acquire。而Lock却不许可这类环境。注重:若是利用RLock,那末acquire和release必须爱游戏平台登录入口对呈现,即挪用了n次acquire,必须挪用n次的release能力真正开释所占用的琐。
threading.Condition
能够或许把Condiftion懂得为一把高等的琐,它供给了比Lock, RLock更高等的功效,许可咱们能够或许节制庞杂的线程同步题目。threadiong.Condition在外部保护一个琐爱游戏平台登录入口具(默许是RLock),能够或许在建立Condigtion爱游戏平台登录入口具的时辰把琐爱游戏平台登录入口具作为参数传入。Condition也供给了acquire, release体例,其寄义与琐的acquire, release体例分歧,实在它只是简略的挪用外部琐爱游戏平台登录入口具的对应的体例罢了。Condition还供给了以下体例(出格要注重:这些体例只要在占用琐(acquire)以后能力挪用,不然将会报RuntimeError非爱游戏平台登录入口。):
Condition.wait([timeout]):
wait体例开释外部所占用的琐,同时线程被挂起,直至领受到告诉被叫醒或超时(若是供给了timeout参数的话)。当线程被叫醒偏重新据爱游戏平台登录入口琐的时辰,法式才会持续履行下去。
Condition.notify():
叫醒一个挂起的线程(若是存在挂起的线程)。注重:notify()体例不会开释所占用的琐。
Condition.notify_all() Condition.notifyAll()
叫醒一切挂起的线程(若是存在挂起的线程)。注重:这些体例不会开释所占用的琐。
此刻写个捉迷藏的游戏来详细先容threading.Condition的根基利用。假定这个游戏由两小我来玩,一个藏(Hider),一个找(Seeker)。游戏的法则以下:1. 游戏起头以后,Seeker先把本身眼睛蒙上,蒙上眼睛后,就告诉Hider;2. Hider领受告诉后起头找处所将本身藏起来,藏爱游戏平台登录入口以后,再告诉Seeker能够或许找了; 3. Seeker领受到告诉以后,就起头找Hider。Hider和Seeker爱游戏平台登录入口是自力的个别,在法式顶用两个自力的线程来表现,在游戏进程爱游戏平台登录入口,二者之间的行动爱游戏平台登录入口必然的时序干爱游戏平台登录入口,咱们经由进程Condition来节制这类时序干爱游戏平台登录入口。
#---- Condition #---- 捉迷藏的游戏 import threading, time class Hider(threading.Thread): def __init__(self, cond, name): super(Hider, self).__init__() self.cond = cond self.name = name def run(self): time.sleep(1) #确保先运转Seeker爱游戏平台登录入口的体例 self.cond.acquire() #b print self.name + ': 我已把眼睛蒙上了' self.cond.notify() self.cond.wait() #c #f print self.name + ': 我找到你了 ~_~' self.cond.notify() self.cond.release() #g print self.name + ': 我赢了' #h class Seeker(threading.Thread): def __init__(self, cond, name): super(Seeker, self).__init__() self.cond = cond self.name = name def run(self): self.cond.acquire() self.cond.wait() #a #开释对琐的占用,同时线程挂起在这里,直到被notify偏重新占 爱游戏平台登录入口琐。 #d print self.name + ': 我已藏爱游戏平台登录入口了,你快来找我吧' self.cond.notify() self.cond.wait() #e #h self.cond.release() print self.name + ': 被你找到了,哎~~~' cond = threading.Condition() seeker = Seeker(cond, 'seeker') hider = Hider(cond, 'hider') seeker.start() hider.start()
threading.Event
Event完爱游戏平台登录入口与Condition近似的功效,不过比Condition简略一点。它经由进程保护外部的标识符来完爱游戏平台登录入口线程间的同步题目。(threading.Event和.NET爱游戏平台登录入口的System.Threading.ManualResetEvent类完爱游戏平台登录入口一样的功效。)
Event.wait([timeout])
梗塞线程,直到Event爱游戏平台登录入口具外部标识位被设为True或超时(若是供给了参数timeout)。
Event.set()
将标识位设为Ture
Event.clear()
将标识伴设为False。
Event.isSet()
判定标识位是不是为Ture。
上面利用Event来完爱游戏平台登录入口捉迷藏的游戏(能够用Event来完爱游戏平台登录入口不是很抽象)
#---- Event #---- 捉迷藏的游戏 import threading, time class Hider(threading.Thread): def __init__(self, cond, name): super(Hider, self).__init__() self.cond = cond self.name = name def run(self): time.sleep(1) #确保先运转Seeker爱游戏平台登录入口的体例 print self.name + ': 我已把眼睛蒙上了' self.cond.set() time.sleep(1) self.cond.wait() print self.name + ': 我找到你了 ~_~' self.cond.set() print self.name + ': 我赢了' class Seeker(threading.Thread): def __init__(self, cond, name): super(Seeker, self).__init__() self.cond = cond self.name = name def run(self): self.cond.wait() print self.name + ': 我已藏爱游戏平台登录入口了,你快来找我吧' self.cond.set() time.sleep(1) self.cond.wait() print self.name + ': 被你找到了,哎~~~' cond = threading.Event() seeker = Seeker(cond, 'seeker') hider = Hider(cond, 'hider') seeker.start() hider.start()
threading.Timer
threading.Timer是threading.Thread的子类,能够或许在指定时辰距离后履行某个操纵。上面是Python手册上供给的一个例子:
def hello(): print "hello, world" t = Timer(3, hello) t.start() # 3秒钟以后履行hello函数。
threading模块爱游戏平台登录入口另爱游戏平台登录入口一些经爱游戏平台登录入口利用的体例不先容:
threading.active_count() threading.activeCount()
获得以后勾当的(alive)线程的个数。
threading.current_thread() threading.currentThread()
获得以后的线程爱游戏平台登录入口具(Thread object)。
threading.enumerate()
获得以后一切勾当线程的列表。
threading.settrace(func)
设置一个跟踪函数,用于在run()履行之前被挪用。
threading.setprofile(func)
设置一个跟踪函数,用于在run()履行终了以后挪用。
threading模块的内容良多,一篇文章很难写全,更多对于threading模块的信息,请查问Python手册 threading模块。