Skip to content

Instantly share code, notes, and snippets.

@maxwellamaral
Created November 16, 2022 12:32
Show Gist options
  • Save maxwellamaral/c1a356e8feac03b70309239a5d0439e4 to your computer and use it in GitHub Desktop.
Save maxwellamaral/c1a356e8feac03b70309239a5d0439e4 to your computer and use it in GitHub Desktop.
Exemplo de multiprocessamento
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