alphanumeric
alphanumeric

Reputation: 19329

Multiprocessing Error

On Windows platform these two lines cause Python code execution to run in infinite loop with every loop starting from the beginning of the script:

import multiprocessing as mp
manager=mp.Manager()

It runs fine on Mac. What would be a reason? It successfully imports import multiprocessing as mp. But on manager=mp.Manager() it breaks (with no errors) and jumps back to the first line of the code.

Upvotes: 0

Views: 100

Answers (1)

dano
dano

Reputation: 94871

On Windows, you have to protect calls to multiprocessing with if __name__ == "__main__":. See here for details on why this is required. This should work fine:

import multiprocessing as mp

if __name__ == "__main__":
    manager=mp.Manager()

Edit:

Note that putting your multiprocessing code inside functions you call from the if block is ok, too. Like this:

import multiprocessing as mp

def func(x):
   print("Got %s" % (x,))

def main():
  p = multiprocessing.Pool()
  results = p.map(func, range(0,5))

if __name__ == "__main__":
  manager = mp.Manager()
  main()

Upvotes: 2

Related Questions