Python的threading和multiprocessing

Python的threading

基礎用法, 經過 threading.Thread() 建立線程, 而後 start() 和 join()python

import time
import threading

def do_something(seconds):
	print('Sleeping...')
	time.sleep(seconds)
	print('Done')

start = time.perf_counter()
threads = []

for _ in range(10):
	t = threading.Thread(target = do_something, args=[1])
	t.start()
	threads.append(t)

for t in threads:
	t.join()

finish = time.perf_counter()
print('Total: {}'.format(round(finish - start, 2)))

使用線程池. 使用as_completed, 能夠阻塞並按完成順序輸出結果, 而直接用executor.map()會將結果收集完成後一塊兒返回.app

import time
import threading
from concurrent import futures

def do_something(seconds):
	print('Sleeping...')
	time.sleep(seconds)
	return 'Done ' + str(seconds)

start = time.perf_counter()
with futures.ThreadPoolExecutor(max_workers=3) as executor:
	secs = [3, 2.5, 2, 2.2, 0.5]
	results = [executor.submit(do_something, sec) for sec in secs]
	for f in futures.as_completed(results):
		print(f.result())

# 注意區別
with futures.ThreadPoolExecutor() as executor:
	secs = [3, 2.5, 2, 2.2, 0.5]
	# 下面這行實際是阻塞的
	results = executor.map(do_something, secs)
	for result in results:
		print(result)

finish = time.perf_counter()
print('Total: {}'.format(round(finish - start, 2)))

.線程

Python的multiprocessing

.在使用multiprocessing時, 子進程裏的print()是會滯後打印的.orm

import time
import multiprocessing
import logging

def do_something(seconds):
	print('Sleeping...', seconds)
	time.sleep(seconds)
	return 'Done ' + str(seconds)

if __name__ == '__main__':
	multiprocessing.log_to_stderr()
	logger = multiprocessing.get_logger()
	logger.setLevel(logging.INFO)
	start = time.perf_counter()
	secs = [3.1, 3.5, 3.1, 3.2, 3.5, 3.3]
	processes = []
	for sec in secs:
		p = multiprocessing.Process(target=do_something, args=(sec,))
		p.start()
		processes.append(p)

	for p in processes:
		p.join()

	finish = time.perf_counter()
	print('Total: {}'.format(round(finish - start, 2)))
	print()

	pool = multiprocessing.Pool(processes=3)
	print(pool.map(do_something, secs))
	finish = time.perf_counter()
	print('Total: {}'.format(round(finish - start, 2)))

.blog

相關文章
相關標籤/搜索