Created
December 10, 2022 23:49
-
-
Save hsleonis/170c1960ed0a9265304250513f03f567 to your computer and use it in GitHub Desktop.
Multiprocessor Pool API
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
from multiprocessor import Pool | |
# multiprocessor.Pool API allows to distribute workload over several processes | |
# Function to apply a function over multiple cores | |
@print_timing | |
def parallel_apply(apply_func, groups, nb_cores): | |
with Pool(nb_cores) as p: | |
results = p.map(apply_func, groups) | |
return pd.concat(results) | |
# Parallel apply using 1 core | |
parallel_apply(take_mean_age, athlete_events.groupby('Year'), 1) | |
# Parallel apply using 2 cores | |
parallel_apply(take_mean_age, athlete_events.groupby('Year'), 2) | |
# Parallel apply using 4 cores | |
parallel_apply(take_mean_age, athlete_events.groupby('Year'), 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment