2013-08-24 105 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

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