[WinError 32] The process cannot access the file because it is being used by another process
Whenever I try to call image.delete(), (being image an instance of ImageFieldFile) I get that error. The image file gets locked. Problem dissapears when I remove the ImageSpecField from the model.
This is the model:
class ModuleImage(models.Model):
module = models.ForeignKey(Module, verbose_name='Módulo', on_delete=models.CASCADE, related_name='images')
image = models.ImageField('Imagen', upload_to='gallery_images')
thumbnail = ImageSpecField(
source='image',
processors=[ResizeToFill(100, 50)],
format='JPEG',
options={'quality': 75})
And the error:
PermissionError at /admin/main/module/34/change/ [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\xleon\Projects\PriceConfigurator\media\gallery_images\IMG_20170505_212351.jpg'
I´m doing this in a post_delete signal:
@receiver(post_delete, sender=ModuleImage)
def module_image_post_delete(sender, instance, *args, **kwargs):
utils.delete_imagefield_file(instance, 'image')
And this is the deleting code:
def delete_imagefield_file(instance, image_field='image', save=False):
if not instance.pk:
return False
image = getattr(instance, image_field)
if os.path.isfile(image.path):
image.delete(save=save)
Thank you for your report but I don't have Windows where to test this.
From what I know the difference between Windows and POSIX based OS is that Windows is locking the file when it is opened and can't be deleted or modified (or even opened again but I'm not sure about that) from another process.
When you say that when ImageSpecField is removed then the problem is gone means that for some reason the file was opened by ImageKit and not closed where it should.
Because as I said I don't have Windows machine I will appreciate if you can investigate the problem and propose a PR with fix for that issue.
If you confirm that this won´t happen on linux enviroments it would be enough for me as the production machine will be Ubuntu. I´m not sure if someone is deploying django to windows servers, but anyway I´ll try to debug this to find the source of the problem.
Yes it will not happen on Ubuntu.
I also think that nobody (or almost nobody) deploy Django on Windows server, but in case of yours - development will be good to be fixed.