Is there a way to check if a module is loaded with a standard multiprocessing module in Windows?

I believe that on Windows, since there is no plug, the multiprocessing module reloads the modules in new Python processes.

You must have this code in your main script, otherwise very nasty crashes

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

I have a bunch of modules that have debug print statements in them at the module level. Therefore, print statements are invoked whenever a module is loaded.

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

My question is, is there a way to see if a module is being imported by a multiprocessing module, and if so it disables these print statements?

I basically look to see if there is something like:

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

I could not find it so far. Is it possible?

+4
source share
2 answers

Yes , a multiprocessor module provides the ability to check whether the module is running 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 
+4
source

Yes, you can get information about the current process from the instance returned by multiprocessing.current_process() . In particular, the Process constructor has a name argument that can be used to distinguish between child processes.

Please note that in python2, if you did not explicitly specify a name , then the module does not provide any guarantee in the format used, therefore you cannot reliably distinguish between subprocesses and the main process: you should always specify it explicitly.

In python3 child processes, it is guaranteed that the name is in the format Process-N with an N positive integer. Note that there is no guarantee of the name of the parent process, so do:

 process.name == 'MainProcess' 

Not reliable. You must do:

 import re re.match(r'Process-\d+', process.name) 
0
source

Source: https://habr.com/ru/post/1496875/


All Articles