Replies: 4 comments 2 replies
-
Hi, thanks for the question. I'll assume that anyone reading this knows about the GIL and why it means that multithreading gives you no benefits when you're running "vanilla Python code". However, multithreading does help when you have threads that are waiting for something, typically for events or for I/O to finish. Before the thread sleeps, it releases the GIL, so other Python threads can now run. When you call into a large C/C++ library it appears to have the same effect. In this case, while all the JPEG encode is happening in libjpegturbo, the GIL is released and another thread can now run Python code (and it too, may release the GIL when it dives into the JPEG encoder...). You can experiment by watching |
Beta Was this translation helpful? Give feedback.
-
Thanks David for your answer, I learned something. |
Beta Was this translation helpful? Give feedback.
-
Indeed, now that's a good question. Truth is, I don't really know, and I believe it's up to whoever implemented the Python bindings. However, I think that both OpenCV and numpy implement this behaviour, but one would have to try it to be sure... |
Beta Was this translation helpful? Give feedback.
-
So I look at the MultiEncoder code that uses ThreadPoolExecutor. |
Beta Was this translation helpful? Give feedback.
-
From the source code:
""This is a base class for a multi-threaded software encoder."""
"Derive your encoder from this class and add an encode_func method. For an example, see JpegEncoder (jpeg_encoder.py).
The parallelism is likely to help when the encoder in question releases the GIL, for example before diving into a large C/C++ library."
I don't completely understand the comment, under what condition is there really parallelism without the GIL.
Is it really the case when the derived encoder for example JpegEncoder calls simplejpeg.encode_jpeg (It seems that the simplejpeg package calls libturbojpeg)
Beta Was this translation helpful? Give feedback.
All reactions