반응형
www.youtube.com/watch?v=fKl2JW_qrso&t=185s
멀티쓰레딩과 거.. 의 똑같다.
키워드만 다를 뿐..
import time
import multiprocessing
start = time.perf_counter()
def 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'Finished in {round(finish-start, 2} second')
그리고 멀티쓰레딩에서 concurrent.futures.ThreadPoolExecutor 대신 이번엔 ProcessPoolExecutor사용하면 됨!
(정말 그렇게 간단하다)
import time
import concurrent.futures
start = time.perf_counter()
def something(s):
print('sleeping..1 second)
time.sleep(s)
return 'done!'
# print('Done sleeping')
with concurrent.futures.ProcessPoolExecutor() as executor:
f1 = executor.submit(something, 1)
f2 = executor.submit(something, 1)
print(f1.result())
print(f2.result())
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2} second')
이러면 GIL(Global Interpretation Lock)에서 벗어나서 여러 프로세스를 여러 코어에 돌릴수 있음!
as_completed 도 사용가능하고,
secs = [5, 4, 3, 2, 1]
results = [executor.submit(something, sec) for sec in secs]
for f in concurrent.futures.as_completed(results):
print(f.result())
map도 가능하다!
results = executor.map(something, secs)
반응형
'Python > Concurrency' 카테고리의 다른 글
Python 멀티쓰레딩 Queue를 사용해 봅시다 (0) | 2021.03.30 |
---|---|
Python Multithreading and Multiprocessing - Multithreading (0) | 2020.10.30 |
Python Threading과 multiprocessing (0) | 2020.10.21 |