Simple Example 1
import time
import threading
start = time.perf_counter()
def do_something():
print('sleeping 1 second...')
time.sleep(1)
print('Done sleeping...')
t1 = threading.Thread(target = do_something)
t2 = threading.Thread(target = do_something)
t1.start()
t2.start()
t1.join()
t2.join()
finish = time.perf_counter()
print(f'Finish in {round(finish-start,2)} seconds(s)')
多个线程的For循环
import time
import threading
start = time.perf_counter()
def do_something():
print('sleeping 1 second...')
time.sleep(1)
print('Done sleeping...')
threads = []
for _ in range(10):
t = threading.Thread(target=do_something)
t.start()
threads.append(t)
for thread in threads:
thread.join()
finish = time.perf_counter()
print(f'Finish in {round(finish-start,2)} seconds(s)')
加个参数
import time
import threading
start = time.perf_counter()
def do_something(seconds):
print(f'sleeping {seconds} second...')
time.sleep(seconds)
print('Done sleeping...')
threads = []
for _ in range(10):
t = threading.Thread(target=do_something, args = [2])
t.start()
threads.append(t)
for thread in threads:
thread.join()
finish = time.perf_counter()
print(f'Finish in {round(finish-start,2)} seconds(s)')
标签:do,start,Python,threading,something,time,print,多线程
From: https://www.cnblogs.com/conpi/p/17061749.html