.Net Framework gcServer mode · pythonnet/pythonnet · Discussion #2053
Hello,
We have a .Net Framework application that performs intensive calculations, and we have found that setting gcServer="true" in our application config file helps with performance (MS Docs).
The calculations can be run via our .net API which we also use through pythonnet. However the performance/speed of execution is slower when running through python, and we suspect this is caused by gcServer mode, which we can see is not enabled when running through Python (GCSettings.IsServerGC return false).
The .net application is multithreaded but doing nothing exotic, there is a main entry point for starting a calculation, and the execution stays in .net until it is completed (so we don't think the GIL could have anything to do with the issue).
We have previously managed to use .net binding redirects by creating a python.exe.config file, but setting the gcServer mode that way does not seem to work.
Any ideas on how we might be able to set gcServer through pythonnet?
You can explicitly specify a configuration file if you use a separate AppDomain, see https://pythonnet.github.io/clr-loader/reference.html#clr_loader.get_netfx. get_netfx is what is called when you do pythonnet.load("netfx", ...), so this should work:
from pythonnet import load load("netfx", domain="some_domain", config_file="some.config") import clr from System.Runtime import GCSettings print(f"{GCSettings.IsServerGC=}")
It doesn't, though :). One issue is easily fixed (I'll do that today), the parameters are not correctly converted. For the other one, that Python.Runtime.dll is not found, I have to look a bit deeper on a Windows system to see how the path resolution differs in the created AppDomain.
I don't see, why the python.exe.config hack wouldn't work, by the way.
2 replies