使用thread
库
import thread thread.start_new_thread(self.getTrafficData, ())
其中getTrafficData是线程开启的函数,因为是写在类里面的,所以加了self
使用threading
库
import threading start_server_thread = threading.Thread(target=self.start_server, args=(self.ip_address, self.apk_filename, self.emails)) start_server_thread.start()
这里也是写在类中的。
[dm href=’http://zhidao.baidu.com/link?url=Ks-z4PcBhRGcobYiWiwqImp7d71Clei2cCvzuNvwvhQinEvrkpiIZh2LT5IKHSFQlmOeanIsRuPLAPxetH8EBsO91YOnKwDwdBsqHklZ7mW’]参考链接[/dm]
示例代码
# -*- coding: utf-8 -*- import threading import thread import time class Test(object): def __init__(self): # threading.Thread.__init__(self) self._sName = "machao" def process(self): #args是关键字参数,需要加上名字,写成args=(self,) th1 = threading.Thread(target=Test.buildList, args=(self,)) th1.start() th1.join() def buildList(self): while True: print "start" time.sleep(3) test = Test() test.process()