Does PIL () change the value of GIL?

Example:

image = Image.open('foo.png') # releases the GIL? resized = image.resize((800, 600), Image.ANTIALIAS) # reacquires the GIL? 

Obviously, the variable assignment must contain a GIL, but it's hard to split into two lines. :)

If there are two threads making image resizing, can these changes resize on two different cores?

+4
source share
2 answers

Looking at source 1.1.7, it does not seem to release a GIL for _resize .

The functions issuing the GIL are as follows:

 PyImaging_CreateWindowWin32 (createwindow on Win32) PyImaging_EventLoopWin32 (eventloop on Win32) pyCMSdoTransform (apply) _buildTransform (buildTransform) _buildProofTransform (buildProofTransform) _encode_to_file (encode_to_file) 
+3
source

From the Python wiki on the GIL :

Note that potentially blocking or lengthy operations, such as I / O, image processing, and NumPy number crunching, occur outside of the GIL. Therefore, only in multi-threaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, does the GIL become a bottleneck.

PIL uses C extensions for most of the hard work. Thus, the actual image resizing should use multithreading, if applicable.

If you are asking about changing multiple images at the same time, I recommend exploring the Python multiprocessing library. This should provide the desired effect of using multiple cores.

+1
source

Source: https://habr.com/ru/post/1444230/


All Articles