Вопрос

Example:

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

Obviously the variable assignment needs to hold the GIL, but it's difficult to break that up into two lines. :)

If there are two threads doing image resizes, can those resizes run on two different cores?

Это было полезно?

Решение

Looking at the source of 1.1.7, it doesn't appear to release the GIL for _resize.

The functions that release the GIL seem to be:

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

Другие советы

From the Python wiki on the GIL:

Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

PIL uses C extensions to do most of its heavy lifting. So the actual image resizing should take advantage of multi-threads if applicable.

If you are asking about resizing multiple images concurrently, I recommend looking into using Python's native multiprocessing library. This should achieve the desired effect of using multiple cores.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top