Is there a way to check if a module is being loaded by multiprocessing standard module in Windows?
Question: I believe on Windows, because there is no _fork_ , the _multiprocessing_
module reloads modules in new Python's processes.

You are required to have this code in your main script, otherwise very nasty
crashes occur

    
    
    if __name__ == '__main__':
        from multiprocessing import freeze_support
        freeze_support()
    

I have a bunch of modules which have debug print statements in them at the
module level. Therefore, the print statements get called whenever a module is
being loaded.

Whenever I run something in parallel all of these print statements are
executed.

My question is if there is a way to see if a module is being imported by the
_multiprocessing_ module, and if so silence those print statements?

I'm basically looking if there is something like:

    
    
     import multiprocessing
     if not multiprocessing.in_parallel_process:
         print('Loaded module: ' + __name___)
    

I've been unable to find it so far. Is this possible?


Answer: **Yes** , the _multiprocessing_ module does provide a way to check whether the
module is being executed in a subprocess or in the main process.

    
    
    from multiprocessing import Process, current_process
    
    if current_process().name == 'MainProcess':
        print('Hello from the main process')
    else:
        print('Hello from child process')
    
    def f(name):
        print('hello', name)
    
    if __name__ == '__main__':
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()
    

Output:

    
    
    Hello from the main process
    Hello from child process
    hello bob
    

