python模块详解 | threading

官方文档 - https://docs.python.org/zh-cn/3/library/threading.html

线程

创建子线程 - threading.Thread()

import threading

def func(i):
  print(i)
  
threading.Thread(target = func, args = (1,)) 

计算运行时间

import threading, time

def target(arg):
  ...

if __name__ == '__main__'  
  start = time.time()
  
  threads = []
  for i in range(5):
    thread = threading.Thread(target = target, args = (i,))
    thread.start()
    threads.append(thread)
  for thread in threads:
    thread.join()

  end = time.time()
  print(f'共耗时:{end-start} s')                      

104 字