0

I took the example from senderle here. I thought I had copy and pasted the main parts of the code, but my code will not stop.

I've tried this example after my own attempts at solving a different problem using pool.map kept hanging. I wonder if there's something actually wrong with the code or my multiprocessing package ...

Below is the code I pulled from senderle's top answer:

from itertools import product
import multiprocessing
def merge_names(a, b):
    return '{} & {}'.format(a, b)
names = ['Brown', 'Wilson', 'Bartlett', 'Rivera', 'Molloy', 'Opie']
with multiprocessing.Pool(processes=3) as pool:
    results = pool.starmap(merge_names, product(names, repeat=2))
print(results)

I was expecting a list of 36 'merged' names, but the process keeps running without end. Any help?

martineau
  • 119,623
  • 25
  • 170
  • 301
Chris
  • 149
  • 1
  • 1
  • 9
  • I tried the original code from senderle's top answer and it also kept hanging. I should add that this was run in the latest version of Jupyter Notebook. HOWEVER, when I ran senderle's code in Spyder, it DID run!! But my code still hung up in Spyder ... – Chris Jul 03 '19 at 23:39
  • 2
    for starters, you should **always** use a `if __name__ == '__main__'` guard when using multiprocessing. There's probably all sorts of complications trying to use multiprocessing in a Jupyter notebook – juanpa.arrivillaga Jul 03 '19 at 23:53

1 Answers1

0

After reading juanpa.arrivillaga's comment, I looked up 'jupyter notebook multiprocessing not working'. First link answered my question.

  1. You must add if __name__ == '__main__' : to your code.

  2. Save the function as a separate py file and import it.

    • In my case, I saved merge_names as merge_names.py
    • Then my code looks like the following:

import multiprocessing from itertools import product import merge_names

if __name__ == '__main__': names = ['Brown', 'Wilson', 'Bartlett', 'Rivera', 'Molloy', 'Opie'] with multiprocessing.Pool(processes=3) as pool: results = pool.starmap(merge_names.merge_names, product(names, repeat=2)) print(results)

Now the code runs in Jupyter Notebook.

Chris
  • 149
  • 1
  • 1
  • 9