Created
November 16, 2022 12:32
-
-
Save maxwellamaral/c1a356e8feac03b70309239a5d0439e4 to your computer and use it in GitHub Desktop.
Exemplo de multiprocessamento
This file contains 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 | |
import time | |
from multiprocessing import Pool, freeze_support | |
MAX_ITER = 9999999 | |
def f1(a): | |
c = 0 | |
for i in range(0, MAX_ITER): | |
c += 1 | |
return 1 | |
def f2(a): | |
c = 0 | |
for i in range(0, MAX_ITER): | |
c += 1 | |
return 1 | |
if __name__ == '__main__': | |
pool = Pool(multiprocessing.cpu_count()) | |
result1 = pool.apply_async(f1, [0]) | |
result2 = pool.apply_async(f2, [9]) | |
freeze_support() | |
t0 = time.time() | |
answer1 = result1.get(timeout=10) | |
answer2 = result2.get(timeout=10) | |
print(time.time() - t0) | |
t0 = time.time() | |
aa = f1(1) | |
bb = f2(2) | |
print(time.time() - t0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment