Skip to content

Instantly share code, notes, and snippets.

@justinhchae
Created March 4, 2021 22:05
Show Gist options
  • Save justinhchae/b774ecb53d228c1bd2457e796fc39925 to your computer and use it in GitHub Desktop.
Save justinhchae/b774ecb53d228c1bd2457e796fc39925 to your computer and use it in GitHub Desktop.
A made up function that calls another function to demonstrate multiprocessing with tqdm progress bar.
# given a function from fc.py
# https://gist.github.com/justinhchae/a2efee420330a985286e7b88fd049ce5
# in a file called main.py
import pandas as pd
# using the pytorch version of mp
import torch.multiprocessing as mp
# we need partial
from functools import partial
# progress bar
from tqdm import tqdm
# import the function fc from
# https://gist.github.com/justinhchae/a2efee420330a985286e7b88fd049ce5
# everything you do, call from inside this module
if __name__ == '__main__':
time.sleep(5)
# lists
a={'a':[5,4,7,2,8]}
b={'b':[8,0,3,2,6]}
c={'c':[2,1,9,7,3]}
d={'d':[6,8,0,4,4]}
# a list of lists
a_list = [a,b,c,d]
# progress bar object of lists
pbar = tqdm(a_list, desc='My Experiment')
# freeze params/function as object
fc_ = partial(fc, param='something')
# set number of processes
p = mp.Pool(8)
# runs mp with params
results2 = list(p.imap(fc_, pbar))
# close out and join processes
p.close()
p.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment