threading 两种调用方法
方法一:建议使用,比较方便简单
import threading
import time
def runthreading(n):
print('run...',n)
time.sleep(2)
t1 = threading.Thread(target=runthreading,args=("threading1",))
t2 = threading.Thread(target=runthreading,args=("threading2",))
t1.start()
t2.start()
方法二:
import threading
import time
class mythreading(threading.Thread):
def __init__(self,n):
#super(mythreading,self).__init__()
threading.Thread.__init__(self)
self.n = n
def run(self):
print('run...',self.n)
t1 = mythreading("t1")
t2 = mythreading("t2")
t1.start()
t2.start()
--------------------------------------------
计算所有线程总的执行时间:
import threading
import time
time_start = time.time()
def runthreading(n):
print('run...',n)
time.sleep(2)
obj_list = []
for i in range(50):
t1 = threading.Thread(target=runthreading,args=("t----%s"%i,))
t1.start()
obj_list.append(t1)
for t in obj_list:
t.join()
print(time.time() - time_start)
运维