Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created December 10, 2022 23:49
Show Gist options
  • Save hsleonis/170c1960ed0a9265304250513f03f567 to your computer and use it in GitHub Desktop.
Save hsleonis/170c1960ed0a9265304250513f03f567 to your computer and use it in GitHub Desktop.
Multiprocessor Pool API
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