Running exiftool within PHP: exiftool never closes and runs high CPU

Started by xberg, February 20, 2017, 11:08:13 AM

Previous topic - Next topic

xberg

Hi,

I run exiftool from within my PHP application whenver a user uploads a photo to my site (about 100x daily) by doing this:


$array = shell_exec('/path/to/exiftool -j '.$filename.'');
$array = (array) current(json_decode($array));


All good so far for the last few years. But every now and then I get server reports that I'm running super high CPU and when I look at the report I see EXIFTOOL hogging the resources:



  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
13404 rrr  20   0  476m 118m  80m R    70  0.5   0:02.71 php-cgi
10038 rrr  20   0  175m 143m 2412 D    51  0.6  15:23.57 exiftool
10082 rrr  20   0  175m 143m 2412 D    51  0.6  15:22.40 exiftool
10043 rrr  20   0  175m 143m 2412 D    38  0.6  15:26.60 exiftool
10094 rrr  20   0  175m 143m 2412 D    38  0.6  15:22.16 exiftool
10085 rrr  20   0  175m 143m 2412 D    32  0.6  15:27.45 exiftool
4935 mysql     20   0 2300m 1.6g 5316 S    25  6.9  67:05.29 mysqld
10040 rrr  20   0  175m 143m 2412 D    25  0.6  15:24.51 exiftool
10087 rrr  20   0  175m 143m 2412 D    25  0.6  15:26.09 exiftool


Now what am I doing wrong? I'm only calling exiftool for a second 100x times a day: this should not create a CPU peak. The chances that even just 2 users submit a photo at the same time is close to 0.

Now the CPU spike does not happen very often: maybe 2x / week and generally cools itself on its own. So perhaps this is a non problem. But still, I think I'm doing this all wrong.
I spent the last hour trying to find a cookbook on implementing exiftool in php but I found nothing that was within my intellectual grasp. Found a few libraries but these confused me even more.

Thanks for any help.

Phil Harvey

Have you taken a look to see what files are being processed when these spikes happen?  Certain PDF files for example can take a significant amount of CPU time (of the order 60 seconds).

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

krzysiu

Please, tell more info:

  • Version of Exiftool
  • Version of PHP (if you can then full phpinfo() dump would be fine; you could remove sensitive data as directories
  • Contents of php.ini

Even if you can't give everything, give at least Exiftool version and PHP version.

In my opinion you should use exec(), not shell_exec() as shell is another layer which is not needed here - you don't use shell commands. I don't know how good you are with PHP, but if you are rather good programmer and you know *nix well, http://php.net/manual/en/function.proc-open.php will give you full control and you could debug it easier.

PS: I code in PHP since PHP3, so a long time and I still prefer own libraries or exec() than these libraries you can find in the Internet. So don't worry, you are not the only one. Recently I had a wonderful adventure in creating gallery system with a lot of exiftool inside it. No external libs needed! If you feel bad with libraries you find, don't feel guilty. There are many kinds of coding styles and maybe they just don't fit you. It's perfectly alright :)
"We would use teleporters and live on another planets, if only ExifTool would be present when I was researching cosmos and physics"
Albert Einstein