0
votes

I have a function to convert a pdf file to a thumbnail after upload.

def book_post_save(sender, instance=False, **kwargs):

     pdf = Post.objects.get(pk=instance.pk)
     command = "convert -quality 95 -thumbnail 100 %s%s[0] %s%s" % (
             settings.MEDIA_ROOT,
             pdf.file,
             settings.MEDIA_ROOT,
             pdf.file
         )
     proc = subprocess.Popen(
             command,
             shell=True,
             stdin=subprocess.PIPE,
             stdout=subprocess.PIPE,
             stderr=subprocess.PIPE,
         )
     stdout_value = proc.communicate()[0]

post_save.connect(book_post_save, sender=Post)

But the subprocess command doesn't return anything. I tried to type it in a terminal but I got a subprocess-error-returned-non-zero-exit-status-1. I got no thumbnail generated.

1
Have you looked at stderr? It is very likely that a useful error message is contained within. - Shadow
No @Shadow , how can I look for it? - Kebson
proc.communicate() returns a tuple - (stdout, stderr). Your code, by having [0] after communicate(), is throwing away stderr. I would suggest doing something like stdout_value, stderr_value = proc.communicate() instead which will allow you to look at the contents of stderr. - Shadow
@Shadow Yeah adding stderr_value, I finally see error and correct it. Thanks - Kebson

1 Answers

1
votes

I think the reason you're struggling here is that you're throwing away your error message.

The communicate method returns a tuple with two elements, stdin and stdout. The code above (namely proc.communicate()[0]) is taking the first element (stdin) but ignoring stderr.

So I would suggest modifying your code to do the following;

stdout_value, stderr_value = proc.communicate()

Once you've done this, if the process fails, you can print stderr_value and work out why the process failed.