您的位置:首页 > 编程语言 > Python开发

Python:threading模块用于多线程控制和处理

2012-11-08 17:53 766 查看

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

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

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'

import threading

lock = threading.Lock() #Lock对象

lock.acquire()

lock.acquire() #产生了死琐。

lock.release()

lock.release()

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()

def hello():

print "hello, world"

t = Timer(3, hello)

t.start() # 3秒钟之后执行hello函数。

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 模块。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python threading模块