Skip to content

Commit 47591ca

Browse files
authored
Release GIL when polling Runtime API for next invocation (#33)
1 parent 620b148 commit 47591ca

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

Diff for: awslambdaric/runtime_client.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@ static PyObject *method_initialize_client(PyObject *self, PyObject *args) {
2525
}
2626

2727
static PyObject *method_next(PyObject *self) {
28-
if (CLIENT == nullptr) {
29-
PyErr_SetString(PyExc_RuntimeError, "Client not yet initalized");
30-
return NULL;
31-
}
28+
aws::lambda_runtime::invocation_request response;
29+
30+
// Release GIL and save thread state
31+
// ref: https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock
32+
PyThreadState *_save;
33+
_save = PyEval_SaveThread();
3234

3335
auto outcome = CLIENT->get_next();
3436
if (!outcome.is_success()) {
37+
// Reacquire GIL before exiting
38+
PyEval_RestoreThread(_save);
3539
PyErr_SetString(PyExc_RuntimeError, "Failed to get next");
3640
return NULL;
3741
}
3842

39-
auto response = outcome.get_result();
43+
response = outcome.get_result();
44+
// Reacquire GIL before constructing return object
45+
PyEval_RestoreThread(_save);
46+
4047
auto payload = response.payload;
4148
auto request_id = response.request_id.c_str();
4249
auto trace_id = response.xray_trace_id.c_str();

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_curl_extra_linker_flags():
3232
def get_runtime_client_extension():
3333
if platform.system() != "Linux" and os.getenv("BUILD") != "true":
3434
print(
35-
"The native runtime_client only builds in Linux. Skiping its compilation."
35+
"The native runtime_client only builds on Linux. Skipping its compilation."
3636
)
3737
return []
3838

0 commit comments

Comments
 (0)