Last active
July 19, 2023 15:50
-
-
Save renatocfrancisco/fd4c1aba67fb0c8177d67d843e257478 to your computer and use it in GitHub Desktop.
Multi processing in Python with mp.Process and concurrent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
import time | |
import concurrent.futures | |
def do_some(seconds = 1): | |
print(f'sleeping {seconds}') | |
time.sleep(seconds) | |
return f'done {seconds}' | |
if __name__ == '__main__': | |
with concurrent.futures.ProcessPoolExecutor() as executor: | |
# -------------- | |
print('concurrent with map') | |
start = time.perf_counter() | |
secs = [5,4,3,2,1] | |
results = executor.map(do_some, secs) | |
for result in results: | |
print(result) | |
finish = time.perf_counter() | |
print(f"Tempo: {round(finish - start,2)} \n") | |
# -------------- | |
print('concurrent with list comprehension') | |
start = time.perf_counter() | |
results = [executor.submit(do_some, sec) for sec in secs] | |
for f in concurrent.futures.as_completed(results): | |
print(f.result()) | |
finish = time.perf_counter() | |
print(f"Tempo: {round(finish - start,2)} \n") | |
# ------------ | |
print('with concurrent') | |
start = time.perf_counter() | |
f1 = executor.submit(do_some, 1) | |
f2 = executor.submit(do_some, 1) | |
print(f1.result()) | |
print(f2.result()) | |
finish = time.perf_counter() | |
print(f"Tempo: {round(finish - start,2)} \n") | |
# # # ------------- | |
print('with multiprocessing (loop)') | |
start = time.perf_counter() | |
processes = [] | |
seconds = [5,4,3,2,1] | |
for i in range(5): | |
p = mp.Process(target=do_some, args=[seconds[i],]) | |
p.start() | |
processes.append(p) | |
for process in processes: | |
process.join() | |
finish = time.perf_counter() | |
print(f"Tempo: {round(finish - start,2)} \n") | |
# # ------------- | |
print('with multiprocessing') | |
start = time.perf_counter() | |
p1 = mp.Process(target=do_some) | |
p2 = mp.Process(target=do_some) | |
p1.start() | |
p2.start() | |
p1.join() | |
p2.join() | |
finish = time.perf_counter() | |
print(f"Tempo: {round(finish - start,2)} \n") |
Author
renatocfrancisco
commented
Jul 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment