Python多线程学习(线程使用)
一.创建线程
1.通过thread模块中的start_new_thread(func,args)创建线程:
在Eclipse+pydev中敲出以下代码:
# -*- coding: utf-8 -*-
import thread
def run_thread(n):
for i in range(n):
print i
thread.start_new_thread(run_thread,(4,)) #参数一定是元组,两个参数可以写成(a,b)
运行报错如下:
Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr
网上查出原因是不建议使用thread,然后我在pythonGUI中做了测试,测试结果如下,显然python是支持thread创建多线程的,在pydev中出错原因暂时不明。
>>> import thread
>>> def run(n):
for i in range(n):
print i
>>> thread.start_new_thread(run,(4,))
98520
1
>>>
2
3
2.通过继承threading.Thread创建线程,以下示例创建了两个线程
# -*- coding: utf-8 -*-
'''''
Created on 2012-8-8
@author: jeromewei
'''
from threading import Thread
import time
class race(Thread):
def __init__(self,threadname,interval):
Thread.__init__(self,name=threadname)
self.interval = interval
self.isrunning = True
def run(self): #重写threading.Thread中的run()
while self.isrunning:
print 'thread %s is running,time:%s\n' %(self.getName(),time.ctime()) #获得线程的名称和当前时间
time.sleep(self.interval)
def stop(self):
self.isrunning = False
def test():
thread1 = race('A',1)
thread2 = race('B',2)
thread1.start()
thread2.start()
time.sleep(5)
thread1.stop()
thread2.stop()
if __name__ =='__main__':
test()
3. 在threading.Thread中指定目标函数作为线程处理函数
# -*- coding: utf-8 -*-
from threading import Thread
def run_thread(n):
for i in range(n):
print i
t1 = Thread(target=run_thread,args=(5,))#指定目标函数,传入参数,这里参数也是元组
t1.start() #启动线程
二. threading.Thread中常用函数说明
函数名 | 功能 |
run() | 如果采用方法2创建线程就需要重写该方法 |
getName() | 获得线程的名称(方法2中有示例) |
setName() | 设置线程的名称 |
start() | 启动线程 |
join(timeout) | 在join()位置等待另一线程结束后再继续运行join()后的操作,timeout是可选项,表示最大等待时间 |
setDaemon(bool) | True:当父线程结束时,子线程立即结束;False:父线程等待子线程结束后才结束。默认为False |
isDaemon() | 判断子线程是否和父线程一起结束,即setDaemon()设置的值 |
isAlive() | 判断线程是否在运行 |
以上方法中,我将对join()和setDaemon(bool)作着重介绍,示例如下:
(1)join方法:
# -*- coding: utf-8 -*-
import threading
import time #导入time模块
class Mythread(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
time.sleep(2)
for i in range(5):
print '%s is running····'%self.getName()
t2 = Mythread('B')
t2.start()
#t2.join()
for i in range(5):
print 'the program is running···'
这时的程序流程是:主线程先运行完,然后等待B线程运行,所以输出结果为:
the program is running···
the program is running···
the program is running···
B is running····
B is running····
B is running····
如果启用t2.join() ,这时程序的运行流程是:当主线程运行到 t2.join() 时,它将等待 t2 运行完,然后再继续运行t2.join() 后的操作,呵呵,你懂了吗,所以输出结果为:
B is running····
B is running····
B is running····
the program is running···
the program is running···
the program is running···
(2)setDaemon方法:
# -*- coding: utf-8 -*-
import threading
import time
class myThread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name=threadname)
def run(self):
time.sleep(5)
print '%s is running·······done'%self.getName()
t=myThread('son thread')
#t.setDaemon(True)
t.start()
if t.isDaemon():
print "the father thread and the son thread are done"
else:
print "the father thread is waiting the son thread····"
这段代码的运行流程是:主线程打印完最后一句话后,等待son thread 运行完,然后程序才结束,所以输出结果为:
the father thread is waitting the son thread····
son thread is running·······done
如果启用t.setDaemon(True) ,这段代码的运行流程是:当主线程打印完最后一句话后,不管 son thread 是否运行完,程序立即结束,所以输出结果为:
the father thread and the son thread are done
三. 小结
介绍到这里,python多线程使用级别的知识点已全部介绍完了,下面我会分析一下python多线程的同步问题。