Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PythonEngine.Shutdown() / Runtime.Shutdown() slowness #2008

Open
@dclaudeone11

Description

@dclaudeone11

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.9.12
  • Operating System: Windows or Linux
  • .NET Runtime: net6.0

Details

  • Describe what you were trying to get done.

We are embedding Python in a .NET app using pythonnet.

The call toPythonEngine.Shutdown() at the exit of this app lasts formore than 2 minutes.

Looking at the shutdown with a profiler shows that most of the time is spent inRuntime.TryCollectingGarbage().

Looks like the more memory is allocated on the C# side the more the shutdown (i.e. calls toGC.Collect()) is slow.

  • Minimal, Complete, and Verifiable example
usingPython.Runtime;namespaceTest;staticclassProgram{staticvoidMain(){varmem=newList<Array>();Runtime.PythonDLL=@$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\Local\Programs\Python\Python39\python39.dll";PythonEngine.Initialize();varthreadState=PythonEngine.BeginAllowThreads();using(Py.GIL()){using(varscope=Py.CreateScope()){scope.Set("a",1);scope.Set("b",2);scope.Exec("result = a + b");varresult=scope.Get("result").As<int>();Console.WriteLine($"Sum ={result}");}}// allocate dotnet-managed memory unrelated to pythonnetvarrnd=newRandom();for(vari=0;i<100_000_000;++i){mem.Add(newbyte[rnd.Next(1,10)]);}PythonEngine.EndAllowThreads(threadState);Console.WriteLine("Shutdown start");varstart=DateTime.Now;PythonEngine.Shutdown();vardiff=DateTime.Now-start;Console.WriteLine($"Shutdown stop{diff.TotalSeconds}");}}

Typical output:

Sum = 3Shutdown startShutdown stop 61.6796983

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp