Python线程编程的两种方式

Java里面有继承Thread和实现Runnable接口,python里面也有同样有两种方法

1.继承threading模块

import threading

class Test(threading.Thread):

def __init__(self,tno):

threading.Thread.__init(self)

self.no=tno

def run(self):

print “Hi i’m thread “+self.no

t1=Test(1)

t2=Test(2)

t1.start()

t2.start()

2.使用thread模块,这个和java中的Timer运行task相似

import thread

def Test(no):

print “i’m in thread “+no

thread.start_new_thread(Test,(1))

thread.start_new_thread(Test,(2))

 

发表评论

邮箱地址不会被公开。 必填项已用*标注