Skip to content

Commit e3ea629

Browse files
author
lemei.huang
committed
add GIL ensure to startInterpreter
1 parent 6ff24c9 commit e3ea629

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@fridgerator/pynode",
3-
"version": "0.5.1",
4-
"description": "Node <-> Python interopability",
3+
"version": "0.5.2",
4+
"description": "Node <-> Python interoperability",
55
"main": "build/Release/PyNode",
66
"author": "Nick Franken <shnick@gmail.com>",
77
"license": "MIT",

src/pynode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Napi::Value StartInterpreter(const Napi::CallbackInfo &info) {
1919
mbstowcs(&path[0], pathString.c_str(), pathString.length());
2020
Py_SetPath(path.c_str());
2121
}
22-
22+
2323
PyImport_AppendInittab("pynode", &PyInit_jswrapper);
2424

2525
int isInitialized = Py_IsInitialized();
@@ -31,6 +31,9 @@ Napi::Value StartInterpreter(const Napi::CallbackInfo &info) {
3131
if (threadsInitialized == 0)
3232
PyEval_InitThreads();
3333

34+
/* Ensure GIL */
35+
py_thread_context ctx;
36+
3437
/* Load PyNode's own module into Python. This makes WrappedJSObject instances
3538
behave better (eg, having attributes) */
3639
PyObject *pName;
@@ -43,9 +46,6 @@ Napi::Value StartInterpreter(const Napi::CallbackInfo &info) {
4346
.ThrowAsJavaScriptException();
4447
}
4548

46-
/* Release the GIL. The other entry points back into Python re-acquire it */
47-
PyEval_SaveThread();
48-
4949
return env.Null();
5050
}
5151

test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ describe('nodePython', () => {
261261
})
262262
})
263263

264+
describe('duplicate startInterpreter', () => {
265+
it('No error should happen when calling startInterpreter again', () => {
266+
nodePython.startInterpreter();
267+
});
268+
})
269+
264270
// describe('stopInterpreter', () => {
265271
// it('should stop the interpreter', () => {
266272
// nodePython.stopInterpreter()

0 commit comments

Comments
 (0)