From 6cf5dea10d05785bde5254d920ce030f5fd03ee7 Mon Sep 17 00:00:00 2001 From: zyxucp <286513187@qq.com> Date: Sat, 20 Jul 2024 18:11:03 +0800 Subject: [PATCH] fix pyruntime --- .../Domain/Other/Bge/BegRerankConfig.cs | 7 +---- .../Domain/Other/Bge/BgeEmbeddingConfig.cs | 8 +----- .../Domain/Other/Bge/PyRunTime.cs | 28 +++++++++++++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/AntSK.Domain/Domain/Other/Bge/PyRunTime.cs diff --git a/src/AntSK.Domain/Domain/Other/Bge/BegRerankConfig.cs b/src/AntSK.Domain/Domain/Other/Bge/BegRerankConfig.cs index bd751f1..ebc9f5c 100644 --- a/src/AntSK.Domain/Domain/Other/Bge/BegRerankConfig.cs +++ b/src/AntSK.Domain/Domain/Other/Bge/BegRerankConfig.cs @@ -26,12 +26,7 @@ namespace AntSK.Domain.Domain.Other.Bge { if (model == null) { - if (string.IsNullOrEmpty(Runtime.PythonDLL)) - { - Runtime.PythonDLL = pythondllPath; - } - PythonEngine.Initialize(); - PythonEngine.BeginAllowThreads(); + PyRunTime.InitRunTime(pythondllPath); try { using (GIL())// 初始化Python环境的Global Interpreter Lock) diff --git a/src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs b/src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs index f345553..f562e3b 100644 --- a/src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs +++ b/src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs @@ -28,13 +28,7 @@ namespace AntSK.Domain.Domain.Other.Bge { if (model == null) { - //Runtime.PythonDLL = @"D:\Programs\Python\Python311\python311.dll"; - if (string.IsNullOrEmpty(Runtime.PythonDLL)) - { - Runtime.PythonDLL = pythondllPath; - } - PythonEngine.Initialize(); - PythonEngine.BeginAllowThreads(); + PyRunTime.InitRunTime(pythondllPath); try { using (GIL())// 初始化Python环境的Global Interpreter Lock) diff --git a/src/AntSK.Domain/Domain/Other/Bge/PyRunTime.cs b/src/AntSK.Domain/Domain/Other/Bge/PyRunTime.cs new file mode 100644 index 0000000..06af3bd --- /dev/null +++ b/src/AntSK.Domain/Domain/Other/Bge/PyRunTime.cs @@ -0,0 +1,28 @@ +using Python.Runtime; + +namespace AntSK.Domain.Domain.Other.Bge +{ + public static class PyRunTime + { + static object lockobj = new object(); + + static bool isInit = false; + + public static void InitRunTime(string pythonPath) + { + lock (lockobj) + { + if (!isInit) + { + if (string.IsNullOrEmpty(Runtime.PythonDLL)) + { + Runtime.PythonDLL = pythonPath; + } + PythonEngine.Initialize(); + PythonEngine.BeginAllowThreads(); + isInit = true; + } + } + } + } +}