This project has moved. For the latest updates, please go here.

How to limit number of CPU cores used by magick.net ?

Nov 26, 2014 at 5:10 PM
Edited Nov 26, 2014 at 5:22 PM
Hi,

While trying to batch resize a lot of images, the CPU is at 95%, witch is great, but not on a system shared with a IIS web server... So, is there a way to limit the number of CPU cores used ?

I tried to set the "MAGICK_THREAD_LIMIT=X" environment variable to limit the number of CPU cores used by Magick.Net but it doesn't seems to work.

I also tried ImageMagick.ResourceLimits.Thread = 2; But it doesn't work too..

Is there a workaround, or a standard way to do that ? ( Thread.Sleep() is not a workaround :)
Dec 5, 2014 at 3:58 PM
This is something I will need pretty soon as well

Subscribing in for a reply when dlemstra comes back..
Coordinator
Dec 9, 2014 at 5:24 PM
Setting ResourceLimits.Thread to 2 will limit the number of threads for a single http request to 2 threads. But even setting this to 1 will not help you. IIS will still use multiple threads/cores to handle all the http requests. If you want to limit this to a single core you should set the processor affinity (http://stackoverflow.com/questions/2510593/how-can-i-set-processor-affinity-in-net). I don't know if you are allowed to do that in a shared hosting environment.

p.s. I am not back, but I have wifi and tablet that I can use to post this message :) I will be back next week.
Dec 9, 2014 at 6:37 PM
There's only one thread running the batch process, so after your answer, I tried but failed to reproduce the issue in a test project.
But by investigating, I found the folowing exception in my logs :

2014-12-09 15:11:30.7349 | FATAL: EXCEPTION: Exception : InvalidOperationException
Failed to load embedded assembly: Accès refusé. (Exception de HRESULT : 0x80070005 (E_ACCESSDENIED))
à ImageMagick.ResourceLimits.set_Thread(UInt64 value)
Magick.NET-AnyCPU

The issue is while setting the ResourceLimits.Thread property, not while using Magick.net. And it only happens while running in IIS, not in VS...
The magick.net assembly seems loaded, as the resize process work correctly...
Coordinator
Dec 9, 2014 at 6:49 PM
Edited Dec 9, 2014 at 6:49 PM
It looks like you are unable to load the embeded 32/64 bit assembly from the AnyCPU version of Magick.NET. Can you try setting MagickAnyCPU.CacheDirectory to a directory you have write access to and test if that resolves the issue? I will improve the error reporting when loading the embedded assembly the next release.
Dec 12, 2014 at 8:48 AM
Now, it works perfectly, thanks for the trick !