Why does pool run the entire file multiple times?

后端 未结 4 1178
南方客
南方客 2020-12-19 06:46

I\'m trying to understand the output from this Python 2.7.5 example script:

import time
from multiprocessing import Pool

print(time.strftime(\'%Y-%m-%d %H:%         


        
相关标签:
4条回答
  • 2020-12-19 07:24

    Python imports the __main__ module for each process. On an import, the whole file is executed again. On python 3, if you remove the if __name__ == '__main__' you will get an infinite loop since the file is getting recursively called.

    For the real question:

    In python scripts, I typically try to avoid executing any statements or variables on the global scope except for function definitions. I use the below as a template for all python scripts.

    import sys
    
    def main(argv):
      #main logic here
    
    if __name__ == '__main__':
      main(sys.argv)
    

    When you have a script with re-usable functions, even if it has a main method, you can import it into another script if you need to.

    0 讨论(0)
  • 2020-12-19 07:26

    Your original post had indenting wrong for:

    print(time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())))  
    

    All "global" statements are run for each process.

    0 讨论(0)
  • 2020-12-19 07:32

    The modification presented below won't print those lines multiple time.

    import time
    
    from multiprocessing import Pool
    
    def main():
      if __name__ == '__main__':
      print(time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())))
      props2=[
            '170339',
            '170357',
            '170345',
            '170346',
            '171232',
            '170363',
            ]
    
      pool = Pool(processes=3)
      pool.map(go, props2)
    
      print(time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())))
    
    def go(x):
      print(x)
    
    main()
    
    0 讨论(0)
  • 2020-12-19 07:40

    Multiprocessing needs to import your script in each subprocess in order to use the go() function. When your script is imported, it prints the date. If you only want something to run in the main script, put it in the if __name__ == '__main__' block.

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