Simple example
import time
import multiprocessing
start = time.perf_counter()
def do_something():
print('sleeping 1 second...')
time.sleep(1)
print('Done sleeping...')
p1 = multiprocessing.Process(target = do_something)
p2 = multiprocessing.Process(target = do_something)
p1.start()
p2.start()
p1.join()
p2.join()
finish = time.perf_counter()
print(f'Finish in {round(finish-start,2)} seconds(s)')
for循环
import time
import multiprocessing
start = time.perf_counter()
def do_something():
print('sleeping 1 second...')
time.sleep(1)
print('Done sleeping...')
processes = []
for _ in range(10):
p = multiprocessing.Process(target = do_something)
p.start()
processes.append(p)
for process in processes:
process.join()
finish = time.perf_counter()
print(f'Finish in {round(finish-start,2)} seconds(s)')
加个参数
import time
import multiprocessing
start = time.perf_counter()
def do_something(seconds):
print(f'sleeping {seconds} second...')
time.sleep(seconds)
print('Done sleeping...')
processes = []
for _ in range(10):
p = multiprocessing.Process(target = do_something, args = [2])
p.start()
processes.append(p)
for process in processes:
process.join()
finish = time.perf_counter()
print(f'Finish in {round(finish-start,2)} seconds(s)')
标签:do,start,Python,print,something,time,进程,multiprocessing
From: https://www.cnblogs.com/conpi/p/17061793.html