-
Notifications
You must be signed in to change notification settings - Fork 750
PythonEngine.Shutdown() / Runtime.Shutdown() slowness #2008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This would probably be helped by handling #1781. If we are finally shutting down, we don't actually have to stash anything, so we also shouldn't try to clean up garbage, right @lostmsu @BadSingleton ? |
Any updates on this @filmor ? |
Apologies for the late reply. Yes, if we're shutting down for the last time, then there is no need to stash anything. If I recall correctly, it used to be that way before when there was still multiple modes. But I'm not sure about garbage collection, I've had some issues in the past (crashing on shutdown?) if it wasn't run correctly. |
@lostmsu @BadSingleton Now that issues like #2107 show up because |
@filmor @BadSingleton do we even need to support restart after shutdown on .NET 7? |
I've been out of the .NET loop for a few months, why wouldn't we need to support it? Otherwise, I don't think it has to be |
Unity is moving to .NET 7 next year supposedly (in large part to support Span). So, it would be best if it were still supported. For the original comment: do we actually need to call Shutdown on shutdown? If not, that would save a lot of time. |
@benoithudson what is Unity on now? I thought it relies on |
Hi, anyway I can work around this issue for now? |
I faced the same problem (Pythonnet 3.0.1, .NET Framework 4.7.2). In my app I have a window with the python code editor. I call PythonEngine.Initialize() when the user opens it, and the PythonEngine.Shotdown() when closing the window. After executing simple scripts, the window closes with a small but noticeable delay. When executing scripts that create more complex objects, for example, matplayotlib figures, the delay increases to tens or even hundreds of seconds. I can call PythonEngine.Shotdown() not when closing the editor window, but when closing the application. But for now this only shifts the problem and does not solve it. Unfortunately, I wasn’t able to find the official position on this subject in the documentation. The fact that many people use Pythonnet suggest that there is a way of bypass, because without it the issue looks serious. @dclaudeone11 Did you manage to find a temporary solution?
@lostmsu First of all, thank you and other contributors for such a useful library. |
@AlexGilmor If you want to shut down Python, use @nlogozzo Since you are not actually using the stashing, you can try setting a no-op formatter: public class NoopFormatter : IFormatter {
public object Deserialize(Stream s) => throw new NotImplementedException();
public void Serialize(Stream s, object o) {}
public SerializationBinder Binder { get; set; }
public StreamingContext Context { get; set; }
public ISurrogateSelector SurrogateSelector { get; set; }
}
Python.Runtime.RuntimeData.FormatterType = typeof(NoopFormatter); |
That worked...thanks! |
Your issue does not occur when the project is ran in .NET 7 using a Release configuration. When ran in Debug configuration, the shutdown does indeed slow down significantly - from 0.08 seconds up to 63 seconds.
This does not fix the issue when the code is ran in Debug configuration under .NET 7. |
v3.0.2 same question, NoopFormatter has no effact |
Environment
Details
We are embedding Python in a .NET app using pythonnet.
The call to
PythonEngine.Shutdown()
at the exit of this app lasts for more than 2 minutes.Looking at the shutdown with a profiler shows that most of the time is spent in
Runtime.TryCollectingGarbage()
.Looks like the more memory is allocated on the C# side the more the shutdown (i.e. calls to
GC.Collect()
) is slow.Typical output:
The text was updated successfully, but these errors were encountered: