From 2435c342c5e72484b875f8940c95d95d4280acd8 Mon Sep 17 00:00:00 2001 From: milliele Date: Sat, 5 Feb 2022 23:32:01 +0800 Subject: [PATCH] add GIL ensure to startInterpreter --- package.json | 4 ++-- src/pynode.cpp | 8 ++++---- test.js | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 4fb802c..97d3128 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@fridgerator/pynode", - "version": "0.5.1", - "description": "Node <-> Python interopability", + "version": "0.5.2", + "description": "Node <-> Python interoperability", "main": "build/Release/PyNode", "author": "Nick Franken ", "license": "MIT", diff --git a/src/pynode.cpp b/src/pynode.cpp index 7b271c8..9474c3d 100644 --- a/src/pynode.cpp +++ b/src/pynode.cpp @@ -19,7 +19,7 @@ Napi::Value StartInterpreter(const Napi::CallbackInfo &info) { mbstowcs(&path[0], pathString.c_str(), pathString.length()); Py_SetPath(path.c_str()); } - + PyImport_AppendInittab("pynode", &PyInit_jswrapper); int isInitialized = Py_IsInitialized(); @@ -31,6 +31,9 @@ Napi::Value StartInterpreter(const Napi::CallbackInfo &info) { if (threadsInitialized == 0) PyEval_InitThreads(); + /* Ensure GIL */ + py_thread_context ctx; + /* Load PyNode's own module into Python. This makes WrappedJSObject instances behave better (eg, having attributes) */ PyObject *pName; @@ -43,9 +46,6 @@ Napi::Value StartInterpreter(const Napi::CallbackInfo &info) { .ThrowAsJavaScriptException(); } - /* Release the GIL. The other entry points back into Python re-acquire it */ - PyEval_SaveThread(); - return env.Null(); } diff --git a/test.js b/test.js index 57cbb39..60c5a17 100644 --- a/test.js +++ b/test.js @@ -261,6 +261,12 @@ describe('nodePython', () => { }) }) + describe('duplicate startInterpreter', () => { + it('No error should happen when calling startInterpreter again', () => { + nodePython.startInterpreter(); + }); + }) + // describe('stopInterpreter', () => { // it('should stop the interpreter', () => { // nodePython.stopInterpreter()