2013-08-24 100 views
1

如果我有一个类,有threading.Thread我运行新线程。开始()多线程类(蟒蛇)

class hello(threading.Thread): 
    def run(): 
     print "hi" 
     print "bye" 

所以这是一个线程,但是当我想里面2个线程功能一个班级?我怎么做?

因为当你使用。开始()它使用一个新的线程运行功能

回答

4

使用target属性的Thread构造来代替:

class twothreads: 
    def t1(self): 
     print "Hi" 

    def t2(self): 
     print "Bye" 

t = twothreads() 
threading.Thread(target=t.t1).start() 
threading.Thread(target=t.t2).start() 
+0

谢谢,不知道它可以被使用的方式 –