使用装饰器开多线程 import time import threading def threadDecorator(func): def wrapper(*args, **kwargs): thread = threading.Thread(target=func, args=args, kwargs=kwargs) thread.start() return thread return wrapper @threadDecorator def demoFunc(): for i in range(6): print(i) time.sleep(1) @threadDecorator def demoFunc02(): for i in range(6): print(time.time()) time.sleep(1) if __name__ == '__main__': demoFunc() demoFunc02() 不使用装饰器和多线程 import time import threading # def threadDecorator(func): # def wrapper(*args, **kwargs): # thread = threading.Thread(target=func, args=args, kwargs=kwargs) # thread.start() # return thread # return wrapper #@threadDecorator def demoFunc(): for i in range(6): print(i) time.sleep(1) #@threadDecorator def demoFunc02(): for i in range(6): print(time.time()) time.sleep(1) if __name__ == '__main__': demoFunc() demoFunc02() (责任编辑:) |