4

例子:

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

显然,变量赋值需要保存 GIL,但很难将其分成两行。:)

如果有两个线程进行图像调整大小,这些调整大小可以在两个不同的内核上运行吗?

4

2 回答 2

3

查看 1.1.7 的源代码,它似乎没有为_resize.

释放 GIL 的函数似乎是:

PyImaging_CreateWindowWin32 (createwindow on Win32)
PyImaging_EventLoopWin32 (eventloop on Win32)
pyCMSdoTransform (apply)
_buildTransform (buildTransform)
_buildProofTransform (buildProofTransform)
_encode_to_file (encode_to_file)
于 2012-12-11T03:00:32.700 回答
1

来自GIL 上的 Python wiki

请注意,潜在的阻塞或长时间运行的操作(例如 I/O、图像处理和 NumPy 数字运算)发生在 GIL 之外。因此,只有在 GIL 中花费大量时间来解释 CPython 字节码的多线程程序中,GIL 才会成为瓶颈。

PIL 使用 C 扩展来完成大部分繁重的工作。因此,如果适用,实际的图像大小调整应该利用多线程。

如果您要同时调整多个图像的大小,我建议您考虑使用 Python 的本机多处理库。这应该可以达到使用多个内核的预期效果。

于 2012-11-06T00:51:33.100 回答