python模块threading
import threading import sys , os from time import ctime , sleep
class MyThread(threading.Thread): def __init__(self, func , args , name=''): threading.Thread.__init__(self) self.func = func self.args = args self.name = name def getResult(self): return self.res def run(self): self.res = apply(self.func , self.args) def loop(): sleep(2) print 'YY'
def loop2(): print 'CC' def main(): t1 = MyThread(loop , () , loop.__name__) t2 = MyThread(loop2 , () , loop2.__name__)
# 注意线程的创建和启动顺序不同,执行顺序不同;当前宏观为同时处理
t1.start() t2.start() t1.join() t2.join() if __name__ == '__main__': main()
最简单直接调用
t1 = threading.Thread(target = myFun,args = ())