Python 标准库提供了 thread 和 threading 两个模块来对多线程进行支持。其中, thread 模块以低级、原始的方式来处理和控制线程,而 threading 模块通过对 thread 进行二次封装,提供了更方便的 api 来处理线程。 虽然使用 thread 没有 threading 来的方便,但它更灵活。今天先介绍 thread 模块的基本使用,下一篇 将介绍 threading 模块。
在介绍 thread 之前,先看一段代码,猜猜程序运行完成之后,在控制台上输出的结果是什么?
[python] view plaincopy
#coding=gbk
import thread, time, random
count = 0
def threadTest():
global count
for i in xrange(10000):
count += 1
for i in range(10):
thread.start_new_thread(threadTest, ()) #如果对start_new_thread函数不是很了解,不要着急,马上就会讲解
time.sleep(3)
print count #count是多少呢?是10000 * 10 吗?
[python] view plaincopy
#coding=gbk
import thread, time
def threadFunc(a = None, b = None, c = None, d = None):
print time.strftime('%H:%M:%S', time.localtime()), a
time.sleep(1)
print time.strftime('%H:%M:%S', time.localtime()), b
time.sleep(1)
print time.strftime('%H:%M:%S', time.localtime()), c
time.sleep(1)
print time.strftime('%H:%M:%S', time.localtime()), d
time.sleep(1)
print time.strftime('%H:%M:%S', time.localtime()), 'over'
thread.start_new_thread(threadFunc, (3, 4, 5, 6)) #创建线程,并执行threadFunc函数。
time.sleep(5)
[python] view plaincopy
import thread, time
thread.start_new_thread(lambda : (thread.interrupt_main(), ), ())
try:
time.sleep(2)
except KeyboardInterrupt, e:
print 'error:', e
print 'over'
[python] view plaincopy
#coding=gbk
import thread, time, random
count = 0
lock = thread.allocate_lock() #创建一个琐对象
def threadTest():
global count, lock
lock.acquire() #获取琐
for i in xrange(10000):
count += 1
lock.release() #释放琐
for i in xrange(10):
thread.start_new_thread(threadTest, ())
time.sleep(3)
print count
threading通过对thread模块进行二次封装,提供了更方便的API来操作线程。
threading.Thread
Thread 是threading模块中最重要的类之一,可以使用它来创建线程。有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法;另一种是创建一个threading.Thread对象,在它的初始化函数(__init__)中将可调用对象作为参数传入。下面分别举例说明。先来看看通过继承threading.Thread类来创建线程的例子:
[python] view plaincopy
#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
[python] view plaincopy
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
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()的使用:
[python] view plaincopy
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'
[python] view plaincopy
import threading
lock = threading.Lock() #Lock对象
lock.acquire()
lock.acquire() #产生了死琐。
lock.release()
lock.release()
[python] view plaincopy
import threading
rLock = threading.RLock() #RLock对象
rLock.acquire()
rLock.acquire() #在同一线程内,程序不会堵塞。
rLock.release()
rLock.release()
[python] view plaincopy
#---- 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()
[python] view plaincopy
#---- 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手册上提供的一个例子:
[python] view plaincopy
def hello():
print "hello, world"
t = Timer(3, hello)
t.start() # 3秒钟之后执行hello函数。