python multiprocessing in Jupyter on Windows: AttributeError: Can't get attribute “abc”

前端 未结 4 1608
夕颜
夕颜 2020-12-06 17:09

I am trying to run a simple command that guesses gender by name using multiprocessing. This code worked on a previous machine so perhaps my setup had something to do with it

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 17:41

    How about this:

    Code:

    #!/usr/bin/env python3
    
    import sys
    import time
    import gender_guesser.detector as gender
    import pandas as pd
    import multiprocessing as mp
    
    d = gender.Detector()
    
    def guess_gender(name):
        n = name.title()
        g = d.get_gender(n)
        return g
    
    def run():
        ls = ['john','joe','amamda','derick','peter','ashley','john',\
              'joe','amamda','derick','peter','ashley']
    
        num_cpus = mp.cpu_count() - 1
        pool = mp.Pool(processes=num_cpus)
        result = pool.map(guess_gender, ls)
    
        df = pd.DataFrame(result, columns=["gender"])
    
        print("\ntook {} secs to classify\n".format(str(time.time() - st)))
        print(df)  # or you could save the dataframe using .to_csv()
    
    st = time.time()
    
    if __name__ == "__main__":
        run()
    

    Output:

    took 0.0150408744812 secs to classify
    
               gender
    0            male
    1            male
    2         unknown
    3            male
    4            male
    5   mostly_female
    6            male
    7            male
    8         unknown
    9            male
    10           male
    11  mostly_female
    

提交回复
热议问题