Skip to content

Release GIL when polling Runtime API for next invocation #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions awslambdaric/runtime_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@ static PyObject *method_initialize_client(PyObject *self, PyObject *args) {
}

static PyObject *method_next(PyObject *self) {
if (CLIENT == nullptr) {
PyErr_SetString(PyExc_RuntimeError, "Client not yet initalized");
return NULL;
}
aws::lambda_runtime::invocation_request response;

// Release GIL and save thread state
// ref: https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock
PyThreadState *_save;
_save = PyEval_SaveThread();

auto outcome = CLIENT->get_next();
if (!outcome.is_success()) {
// Reacquire GIL before exiting
PyEval_RestoreThread(_save);
PyErr_SetString(PyExc_RuntimeError, "Failed to get next");
return NULL;
}

auto response = outcome.get_result();
response = outcome.get_result();
// Reacquire GIL before constructing return object
PyEval_RestoreThread(_save);

auto payload = response.payload;
auto request_id = response.request_id.c_str();
auto trace_id = response.xray_trace_id.c_str();
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_curl_extra_linker_flags():
def get_runtime_client_extension():
if platform.system() != "Linux" and os.getenv("BUILD") != "true":
print(
"The native runtime_client only builds in Linux. Skiping its compilation."
"The native runtime_client only builds on Linux. Skipping its compilation."
)
return []

Expand Down