0

I'm a newbee for multiprocess and try with a multiprocess demo in python.

from multiprocessing import Pool

def foo(a, b):
    print(a,b)
    ...

def bar(a, b):
    print(a,b)
    ...

if __name__ == '__main__':
    pool = Pool(processes=2)
    func_list = [foo, bar]
    for func in func_list:
        pool.apply_async(func, (1, 2))
    pool.close()
    pool.join()

print(123)

When I work with win32 platform

123 output always be 3 times

123
1 2
1 2
123
123

But when try this demo with linux it output once

1 2
1 2
123

Why this print execute 3 times out of main block in python Window?

jia Jimmy
  • 1,693
  • 2
  • 18
  • 38

1 Answers1

1

Thats a limitation of multiprocessing under Windows, because it has no fork. This is why a new process under Windows reimports all modules instead of inheriting the interpreter state from parent process. You have need to move print(123) under the if __name__ == '__main__': guard. See also https://stackoverflow.com/a/24374798/725126

vinzenz
  • 669
  • 3
  • 14