Merge pull request #4433 from zt-initech/release_GIL_in_WEBP_encode

Release Python GIL during WEBP encode
This commit is contained in:
Hugo van Kemenade 2020-03-27 20:08:26 +02:00 committed by GitHub
commit 7b628a5ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,14 @@
#endif #endif
void ImagingSectionEnter(ImagingSectionCookie* cookie) {
*cookie = (PyThreadState *) PyEval_SaveThread();
}
void ImagingSectionLeave(ImagingSectionCookie* cookie) {
PyEval_RestoreThread((PyThreadState*) *cookie);
}
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* WebP Muxer Error Handling */ /* WebP Muxer Error Handling */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
@ -548,6 +556,7 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
Py_ssize_t exif_size; Py_ssize_t exif_size;
Py_ssize_t xmp_size; Py_ssize_t xmp_size;
size_t ret_size; size_t ret_size;
ImagingSectionCookie cookie;
if (!PyArg_ParseTuple(args, "y#iiifss#s#s#", if (!PyArg_ParseTuple(args, "y#iiifss#s#s#",
(char**)&rgb, &size, &width, &height, &lossless, &quality_factor, &mode, (char**)&rgb, &size, &width, &height, &lossless, &quality_factor, &mode,
@ -560,11 +569,15 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
} }
#if WEBP_ENCODER_ABI_VERSION >= 0x0100 #if WEBP_ENCODER_ABI_VERSION >= 0x0100
if (lossless) { if (lossless) {
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeLosslessRGBA(rgb, width, height, 4 * width, &output); ret_size = WebPEncodeLosslessRGBA(rgb, width, height, 4 * width, &output);
ImagingSectionLeave(&cookie);
} else } else
#endif #endif
{ {
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeRGBA(rgb, width, height, 4 * width, quality_factor, &output); ret_size = WebPEncodeRGBA(rgb, width, height, 4 * width, quality_factor, &output);
ImagingSectionLeave(&cookie);
} }
} else if (strcmp(mode, "RGB")==0){ } else if (strcmp(mode, "RGB")==0){
if (size < width * height * 3){ if (size < width * height * 3){
@ -572,11 +585,15 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
} }
#if WEBP_ENCODER_ABI_VERSION >= 0x0100 #if WEBP_ENCODER_ABI_VERSION >= 0x0100
if (lossless) { if (lossless) {
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeLosslessRGB(rgb, width, height, 3 * width, &output); ret_size = WebPEncodeLosslessRGB(rgb, width, height, 3 * width, &output);
ImagingSectionLeave(&cookie);
} else } else
#endif #endif
{ {
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeRGB(rgb, width, height, 3 * width, quality_factor, &output); ret_size = WebPEncodeRGB(rgb, width, height, 3 * width, quality_factor, &output);
ImagingSectionLeave(&cookie);
} }
} else { } else {
Py_RETURN_NONE; Py_RETURN_NONE;