Python:创建多线程(推荐)

import threading, time

def thread_go(s):
    print(s)
    time.sleep(2)

start_time = time.time()
ths = []
for i in range(3):
    t = threading.Thread(target = thread_go, args = ("线程%s" % i, ))
    t.start()
    ths.append(t)

for t in ths:
    t.join() #阻塞直到线程t执行完成
    
print("所有线程执行完成,共耗时:%s" % str(time.time() - start_time))

输出:

线程0
线程1
线程2
所有线程执行完成,共耗时:2.018460273742676
posted @ 2018-12-27 09:00  xuejianbest  阅读(103)  评论(0编辑  收藏  举报