-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Image.crop is not thread safe #4848
Comments
Hi. When I run your code, I mostly see "'NoneType' object has no attribute 'read'". I did get a segfault once. If I insert a line to load the image, try:
image = Image.open("./image.png")
image.load() Then I find that the problem is solved. The reason for this would be that Pillow does not finish reading the file from disk after If you'd like to talk about other errors, please attach the image that you're using. |
Thanks @radarhere this solved the issue for me too. Basically you're saying that I would personally still consider this an issue but if you think it's acceptable we can close it. BTW, is calling |
I wouldn't say that So an alternative way of fixing the problem would be to open the image within the thread - try:
with futures.ThreadPoolExecutor() as executor:
def x(i):
image = Image.open("/Users/andrewmurray/Dropbox/docs/pillow/Pillow/Tests/images/hopper.png")
return executor.submit(crop_image, image, i, i, 10, 10, False)
futures = [x(i) for i in range(10)]
cropped = [future.result() for future in futures]
for i in range(len(cropped)):
cropped[i].save(f"output-{i}.png", "PNG")
except Exception as ex:
print(f"Exception: {ex}") |
Obviously I could open the image in the thread but I wanted to do common work once (under the assumption that it's more performant than opening the image multiple times) Now I'm confused about resource management, do I have to use images in context managers? The code you just posted doesn't do so (nor call I see that
If I load all images I create do I still need to use a context manager? If I don't call |
To be clear, the documentation you quoted comes from Lines 582 to 592 in 27a8852
You should use either a context manager or https://pillow.readthedocs.io/en/stable/reference/open_files.html
|
Testing, I don't find that your particular code leaks multiple file handles, only one. However, https://pillow.readthedocs.io/en/stable/reference/open_files.html#proposed-file-handling
So it does leak a file handle if I change the format to SPIDER. I've created #5239 to clarify the documentation. |
#5239 has been merged, changing the Lines 582 to 592 in 023dbe3
|
As to why a context manager or If you look at #3577, part of the reasoning is to 'close the file handler at a specified point in the user's code'. If you have further questions, let us know. |
What did you do?
Try to run several
Image.crop
actions concurrently.What did you expect to happen?
Success
What actually happened?
Got some errors (here's a partial list)
And even a crash of the Python process.
What are your OS, Python and Pillow versions?
The text was updated successfully, but these errors were encountered: