@@ -2,38 +2,37 @@ import Foundation
22import Python
33import PythonHelper
44import PythonKit
5+ import Logger
56
7+ @MainActor
68func initializePython( ) {
79 guard let sitePackagePath = Bundle . main. path ( forResource: " site-packages " , ofType: nil ) ,
810 let stdLibPath = Bundle . main. path ( forResource: " python-stdlib " , ofType: nil ) ,
911 let libDynloadPath = Bundle . main. path (
1012 forResource: " python-stdlib/lib-dynload " ,
1113 ofType: nil
1214 )
13- else { return }
14-
15- setenv ( " PYTHONHOME " , stdLibPath, 1 )
16- setenv ( " PYTHONPATH " , " \( stdLibPath) : \( libDynloadPath) : \( sitePackagePath) " , 1 )
17-
18- // Initialize python
19- Py_Initialize ( )
20-
21- // Immediately release the thread, so that we can ensure the GIL state later.
22- // We may not recover the thread because all future tasks will be done in the Python Thread.
23- _ = PyEval_SaveThread ( )
15+ else {
16+ Logger . service. info ( " Python is not installed! " )
17+ return
18+ }
2419
25- // Setup GIL state guard.
26- PythonHelper . gilStateEnsure = { PyGILState_Ensure ( ) }
27- PythonHelper . gilStateRelease = { gilState in PyGILState_Release ( gilState as! PyGILState_STATE ) }
20+ PythonHelper . initializePython (
21+ sitePackagePath: sitePackagePath,
22+ stdLibPath: stdLibPath,
23+ libDynloadPath: libDynloadPath,
24+ Py_Initialize: Py_Initialize,
25+ PyEval_SaveThread: PyEval_SaveThread,
26+ PyGILState_Ensure: PyGILState_Ensure,
27+ PyGILState_Release: PyGILState_Release
28+ )
2829
2930 Task {
3031 // All future task should run inside runPython.
3132 try runPython {
3233 let sys = Python . import ( " sys " )
33- print ( " Python Version: \( sys. version_info. major) . \( sys. version_info. minor) " )
34+ Logger . service . info ( " Python Version: \( sys. version_info. major) . \( sys. version_info. minor) " )
3435 }
3536 }
3637}
3738
38- let queue = DispatchQueue ( label: " " )
39-
0 commit comments