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

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.


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

Code in Jupyter notebook:

from multiprocessing import Process, Queue

#Having the function definition here results in
#AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>

#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

   # Setup a list of processes that we want to run
   p = Process(target=f.f, args=('Bob',output))

   # Run process

   # Exit the completed process

   # Get process results from the output queue
   result = output.get(p)


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

Leave a Comment