python3.x multiprocessing cycling without “if __name__ == '__main__':”

后端 未结 2 1548
别那么骄傲
别那么骄傲 2021-01-01 07:05

I have this file (it doesn\'t make any useful work, it\'s only for learning):

import multiprocessing,sys
def parent(numproc=2):
    print (\'at start\')
             


        
相关标签:
2条回答
  • 2021-01-01 07:30

    The subprocess has the following __name__: __parents_main__ and not __main__ any more. This is why your process does not loop when you are testign the __name__ variable.

    For more information on this have a look to the chapter Safe importing of main module

    0 讨论(0)
  • 2021-01-01 07:33

    This is an issue with multiprocessing on MS Windows: the main module is imported by the child tasks, so any code not protected by the if __name__ . . . clause gets run again, resulting in an infinite loop.

    0 讨论(0)
提交回复
热议问题