From 7df7cb25ee4360a00f407692262f3fc8eff27d96 Mon Sep 17 00:00:00 2001 From: Zeev Tarantov Date: Mon, 17 Feb 2020 22:56:30 +0200 Subject: [PATCH 1/2] release python GIL during WEBP encode --- src/_webp.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/_webp.c b/src/_webp.c index 4581ef89d..7b85062a9 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -21,6 +21,16 @@ #endif +void ImagingSectionEnter(ImagingSectionCookie* cookie) +{ + *cookie = (PyThreadState *) PyEval_SaveThread(); +} + +void ImagingSectionLeave(ImagingSectionCookie* cookie) +{ + PyEval_RestoreThread((PyThreadState*) *cookie); +} + /* -------------------------------------------------------------------- */ /* WebP Muxer Error Handling */ /* -------------------------------------------------------------------- */ @@ -555,6 +565,7 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args) Py_ssize_t exif_size; Py_ssize_t xmp_size; size_t ret_size; + ImagingSectionCookie cookie; if (!PyArg_ParseTuple(args, "y#iiifss#s#s#", (char**)&rgb, &size, &width, &height, &lossless, &quality_factor, &mode, @@ -567,11 +578,15 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args) } #if WEBP_ENCODER_ABI_VERSION >= 0x0100 if (lossless) { + ImagingSectionEnter(&cookie); ret_size = WebPEncodeLosslessRGBA(rgb, width, height, 4 * width, &output); + ImagingSectionLeave(&cookie); } else #endif { + ImagingSectionEnter(&cookie); ret_size = WebPEncodeRGBA(rgb, width, height, 4 * width, quality_factor, &output); + ImagingSectionLeave(&cookie); } } else if (strcmp(mode, "RGB")==0){ if (size < width * height * 3){ @@ -579,11 +594,15 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args) } #if WEBP_ENCODER_ABI_VERSION >= 0x0100 if (lossless) { + ImagingSectionEnter(&cookie); ret_size = WebPEncodeLosslessRGB(rgb, width, height, 3 * width, &output); + ImagingSectionLeave(&cookie); } else #endif { + ImagingSectionEnter(&cookie); ret_size = WebPEncodeRGB(rgb, width, height, 3 * width, quality_factor, &output); + ImagingSectionLeave(&cookie); } } else { Py_RETURN_NONE; From b1f22344b4d79421285528dfe2e06bde81ae4d6c Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 26 Mar 2020 19:07:51 +0200 Subject: [PATCH 2/2] Style consistency --- src/_webp.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/_webp.c b/src/_webp.c index 7b85062a9..cbe69187d 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -21,13 +21,11 @@ #endif -void ImagingSectionEnter(ImagingSectionCookie* cookie) -{ +void ImagingSectionEnter(ImagingSectionCookie* cookie) { *cookie = (PyThreadState *) PyEval_SaveThread(); } -void ImagingSectionLeave(ImagingSectionCookie* cookie) -{ +void ImagingSectionLeave(ImagingSectionCookie* cookie) { PyEval_RestoreThread((PyThreadState*) *cookie); }