Chinaunix首页 | 论坛 | 博客
  • 博客访问: 653709
  • 博文数量: 87
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2022
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-23 11:16
个人简介

西邮大三狗!!!

文章分类

全部博文(87)

文章存档

2015年(47)

2014年(40)

分类: Python/Ruby

2015-06-08 13:03:40

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

阅读(3701) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~