When using ImageMagick, I can set certain limits for memory usage and maximum number of threads. There are 3 ways to do this, as far as I know:
I have tested each of these methods using "convert -list resource" and they work.
Now, I need to use the IMagick extension in PHP. There is a function I can use to set limits:
bool Imagick::setResourceLimit (int $type, int $limit)
For the first parameter I can use one of the following:
imagick::RESOURCETYPE_AREA (integer) //equivalent of MAGICK_AREA_LIMIT
imagick::RESOURCETYPE_DISK (integer) //equivalent of MAGICK_DISK_LIMIT
imagick::RESOURCETYPE_FILE (integer) //equivalent of MAGICK_FILE_LIMIT
imagick::RESOURCETYPE_MAP (integer) //equivalent of MAGICK_MAP_LIMIT
imagick::RESOURCETYPE_MEMORY (integer) //equivalent of MAGICK_MEMORY_LIMIT
The problem is that there is no equivalent for MAGICK_THREAD_LIMIT and IMagick seems to simply ignore the configuration files and the environment variables. How do I know this? I've set all the memory limits to zero and IMagick still functions without any problem when it should report insufficient memory.
I really hope I have made myself clear. The question is: how can I change the thread limit when using IMagick?
EDIT: I've managed to set the thread limit to 1 by compiling ImageMagick with the '--without-threads' option. :P It will have to do until I find a better solution.
There is no corresponding constant defined for the thread limit in the PHP IMagick extension, but looking at the source the integer value should be 6 so you could try that (see ResourceType in magick/resource_.h, the needed value is ThreadResource). Am using MagickWand for PHP and had the same issue--fix was to enable this constant and recompile. If you are interested in patching MagickWand for PHP 1.0.8 the fix is:
magickwand_inc.h
-#define PRV_IS_ResourceType( x ) (x == AreaResource || x == DiskResource || x == FileResource || x == MapResource || x == MemoryResource) /* || x == UndefinedResource */
+#define PRV_IS_ResourceType( x ) (x == AreaResource || x == DiskResource || x == FileResource || x == MapResource || x == MemoryResource || x == ThreadResource) /* || x == UndefinedResource */
magickwand.c
MW_REGISTER_LONG_CONSTANT( MemoryResource );
+ MW_REGISTER_LONG_CONSTANT( ThreadResource );
It is possible to pass the MAGICK_THREAD_LIMIT environment variable to PHP interpreter so you don't need to touch the ImageMagick code.
Check this out: High CPU load when converting images with ImageMagick
This helped:
Imagick::setResourceLimit (6, 1);
At ImageMagick version 6.8.7-4, setResourceLimit(6,1) doesn't help, neither MAGICK_THREAD_LIMIT=1.
But this setting does the trick:
OMP_THREAD_LIMIT=1
In CLI:
env OMP_THREAD_LIMIT=1 php ./myscript.php