桀昊j 发表于 2015-12-15 10:03:55

python标准库学习—threading模块

Thread类
Thread(group = none, target = none, name = none, args = [], kwargs = ())
用于表示单独的控制线程
实例常用方法:
t.start()            通过在一个单独的控制线程中调用run()方法,启动线程,只能调用一次
t.run()            线程启动时调用此方法
t.join()             等待直到线程终止或者出现超时为止
t.is_alive()      线程是活的。返回True
t.name             线程名称
t.ident             整数线程标识符
t.daemon      线程的布尔型后台标志


example:
#!/usr/bin/env python


import threading
import time


class mythreading(threading.Thread):
def __init__(self,name,sec):
threading.Thread.__init__(self)
self.name = name
self.sec =sec


def run(self):
times = 1
print "%s is running" % self.name
while self.sec:
   print "runnig %d times" % times
   times += 1
   self.sec -= 1
   time.sleep(2)
print "%s is stoped" % self.name


if __name__ == '__main__':
mythread = mythreading('nick01',5)
mythread.start()
页: [1]
查看完整版本: python标准库学习—threading模块