Initialize initializes the Python interpreter and its global interpreter lock (GIL).
()
| 43 | |
| 44 | // Initialize initializes the Python interpreter and its global interpreter lock (GIL). |
| 45 | func Initialize() error { |
| 46 | if C.Py_IsInitialized() == 0 { |
| 47 | // initialize the interpreter without signal handlers |
| 48 | C.Py_InitializeEx(0) |
| 49 | } |
| 50 | if C.Py_IsInitialized() == 0 { |
| 51 | return ErrPyInit |
| 52 | } |
| 53 | if C.PyEval_ThreadsInitialized() == 0 { |
| 54 | C.PyEval_InitThreads() |
| 55 | } |
| 56 | if C.PyEval_ThreadsInitialized() == 0 { |
| 57 | return ErrGILInit |
| 58 | } |
| 59 | // this calls into PyDateTime_IMPORT macro to initialize the PyDateTimeAPI |
| 60 | C.Py_DateTimeImport() |
| 61 | |
| 62 | if err := initializeIPFnAndClasses(); err != nil { |
| 63 | log.Warn(err) |
| 64 | } |
| 65 | |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // Finalize undos all initializations made by Initialize() and subsequent use of Python/C API functions, |
| 70 | // and destroys all sub-interpreters that were created and not yet destroyed since the last call to Initialize(). |