11 lines
572 B
Python
11 lines
572 B
Python
from concurrent.futures import ProcessPoolExecutor
|
|
from mc import count_hits
|
|
def estimate_pi_parallel(n_points, pool, n_workers):
|
|
"""Делит работу на n_workers порций и считает их в пуле процессов."""
|
|
chunk = n_points // n_workers
|
|
sizes = [chunk] * n_workers
|
|
sizes[-1] += n_points - chunk * n_workers # остаток — последней порции
|
|
seeds = range(1, n_workers + 1) # разные зёрна у порций
|
|
hits = sum(pool.map(count_hits, sizes, seeds))
|
|
return 4 * hits / n_points
|