🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
单线程和多线程对比 ```python #coding=utf-8 from threading import Thread from time import sleep, ctime from atexit import register class MyThread(Thread): def __init__(self, target=None, name=None, args=()): Thread.__init__(self) self.target = target self.name = name self.args = args def getResult(self): return self.res def run(self): print 'start %s at %s' % (self.target.__name__, ctime()) self.res = self.target(*self.args) print 'stop %s at %s' % (self.target.__name__, ctime()) def fib(x): sleep(0.005) if x < 2: return 1 return fib(x-2) + fib(x-1) def fac(x): sleep(0.1) if x < 2: return 1 return x * fac(x-1) def sum(x): sleep(0.1) if x < 2: return 1 return x + sum(x-1) funcs=[fib, fac, sum] n = 12 def main(): print '**- single thread -**' for i in funcs: print 'start %s at %s' % (i.__name__, ctime()) print i(n) print 'stop %s at %s' % (i.__name__, ctime()) print '**- multiple thread -**' threads = [] for i in funcs: t = MyThread(i, args=(n,)) threads.append(t) for t in threads: t.start() for t in threads: t.join() print t.getResult() # 这个函数会在脚本退出是运行 @register def _atexit(): print 'all DONE at: %s' % ctime() if __name__ == '__main__': main() ```