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

2024/11/17 14:28:45

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_supportfreeze_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 multiprocessingif not multiprocessing.in_parallel_process:print('Loaded module: ' + __name___)

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

Answer

For Python 3.8 and newer use multiprocessing.parent_process() which returns None only for the main process and for earlier versions check if name attribute's value equals MainProcess.

from multiprocessing import Process, current_processif 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
https://en.xdnf.cn/q/71185.html

Related Q&A

Condas solving environment takes forever

I am using conda since one year, since several weeks, whenever I want to install a package using conda install -c anaconda <package_name>, for any package, it is just stuck at the Solving environ…

How to overwrite a file in Python?

Im trying to overwrite a file. I based my answer on this Read and overwrite a file in PythonTo complete my codes:<select class="select compact expandable-list check-list" ONCHANGE="lo…

Is os.popen really deprecated in Python 2.6?

The on-line documentation states that os.popen is now deprecated. All other deprecated functions duly raise a DeprecationWarning. For instance:>>> import os >>> [c.close() for c in os…

Routes with trailing slashes in Pyramid

Lets say I have a route /foo/bar/baz. I would also like to have another view corresponding to /foo or /foo/. But I dont want to systematically append trailing slashes for other routes, only for /foo a…

understanding item for item in list_a if ... PYTHON

Ive seen the following code many times, and I know its the solution to my problems, but Im really struggling to understand HOW it works. The code in particular is:item for item in list_a if item not in…

Django redirect to custom URL

From my Django app, how to I redirect a user to somescheme://someurl.com?To give you some context in case it helps, I have a working oauth2 server written in Python/Django and I need to allow users to…

Python embedding with threads -- avoiding deadlocks?

Is there any way to embed python, allow callbacks from python to C++, allowing the Pythhon code to spawn threads, and avoiding deadlocks?The problem is this:To call into Python, I need to hold the GIL…

RuntimeError: Event loop is closed when using pytest-asyncio to test FastAPI routes

I received the errorRuntimeError: Event loop is closedeach time I try to make more than one async call inside my test. I already tried to use all other suggestions from other Stack Overflow posts to re…

Adjust threshold cros_val_score sklearn

There is a way to set the threshold cross_val_score sklearn?Ive trained a model, then I adjust the threshold to 0.22. The model in the following below :# Try with Threshold pred_proba = LGBM_Model.pre…

Efficiently insert multiple elements in a list (or another data structure) keeping their order

I have a list of items that should be inserted in a list-like data structure one after the other, and I have the indexes at which each item should be inserted. For example: items = [itemX, itemY, itemZ…