Multiprocessing hangs forever in Jupyterlab on Windows 11

I try the hello-world example as follows of multiprocessing in Jupyterlab and it hangs forever, and I Googled and tried different ways still no luck, please help and advise, thanks in advance.

from multiprocessing import Pool

def f(x):
return x*x

if name == ‘main’:
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))

Is that what you ran or something like it? When I run that I get:

NameError: name 'name' is not defined

Did you use?

if __name__ == "__main__":

If so, then then searching ‘jupyter multiprocessing windows 11’ brings up this answer here to ’ Python multiprocessing windows and jupyter’ for the same function you are trying to run with multiprocessing in Jupyter running on Windows:

" Try put the function square in a separate file then import it, as mentioned in this post"

(Indeed, suggesting this is on the correct track, the referenced post that it is cited as duplicating sounds a lot like your description: Jupyter notebook never finishes processing using multiprocessing (Python 3).)

In that post referenced with the answer is a subtle variation approach using a temporary file that is made in more of a programmatic manner, too. Still another, lower-scoring answer there says it isn’t supported, which maybe is why you have to expect to jump through some hoops to use it in Windows. The fact that the other options score higher makes me think if you go through the extra steps, you can indeed get it working on Windows. (I have not tested it though on Windows myself.)


For pasting code in so it is readable in forums such as this, you’ll want to learn about ‘block code formatting’ here. (Or see about ‘fenced code blocks’ here. They are both the same thing if you look into the details. They just have slightly different terms used in the two locations.)
And a good practice is to always open a new notebook with a fresh kernel and run the minimal reproducible example you supply before hitting ‘post’. Represent accurately what you tried and help those trying to help you.