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

前端 未结 4 1620
夕颜
夕颜 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:18

    I got multiprocessing to work from within a Jupyter notebook on Windows by saving my function in a separate .py file and including that file in my notebook.

    Example:

    f.py:

    def f(name, output):
      output.put('hello {0}'.format(name))
      return
    

    Code in Jupyter notebook:

    from multiprocessing import Process, Queue
    
    #Having the function definition here results in
    #AttributeError: Can't get attribute 'f' on 
    
    #The solution seems to be importing the function from a separate file.
    
    import f
    
    #Also, the original version of f only had a print statement in it.  
    #That doesn't work with Process - in the sense that it prints to the console 
    #instead of the notebook.
    #The trick is to let f write the string to print into an output-queue.
    #When Process is done, the result is retrieved from the queue and printed.
    
    if __name__ == '__main__':    
    
       # Define an output queue
       output=Queue()
    
       # Setup a list of processes that we want to run
       p = Process(target=f.f, args=('Bob',output))
    
       # Run process
       p.start()
    
       # Exit the completed process
       p.join()
    
       # Get process results from the output queue
       result = output.get(p)
    
       print(result)
    

    I'm a Python newby and I may have missed all sorts of details, but this works for me.

提交回复
热议问题