Pass MAGICK_THREAD_LIMIT to PerlMagick

huangapple go评论95阅读模式
英文:

Pass MAGICK_THREAD_LIMIT to PerlMagick

问题

我在使用受限用户帐户下的PerlMagick时遇到了一个问题,出现了以下错误:

libgomp: Thread creation failed: Resource temporarily unavailable

我发现可以通过以下两种方法来修复这个问题:通用环境变量设置 export OMP_NUM_THREADS=1 或者 ImageMagick特定的设置 export MAGICK_THREAD_LIMIT=1。但是,我想将这个修正集成到Perl本身中,要么通过直接传递配置给PerlMagick(这似乎是“正确”的修复方式),要么通过在脚本中设置环境变量。

我尝试设置$ENV{'OMP_NUM_THREADS'},但没有解决问题,即使在运行脚本之前设置该变量也能解决问题。同样地,尝试在脚本内部运行export命令也没有效果。

有没有更好的方法来纠正这个问题?

英文:

I've run into an issue using PerlMagick under a restricted user account where this error occurs:

libgomp: Thread creation failed: Resource temporarily unavailable

I discovered that this can be fixed using either the general environmental variable setting export OMP_NUM_THREADS=1 or the ImageMagick specific one export MAGICK_THREAD_LIMIT=1. However, I'd like to integrate the correction into Perl itself, either by passing a configuration directly to PerlMagick (that seems like the "correct" fix) or by setting the environmental variable in the script.

I tried setting $ENV{'OMP_NUM_THREADS'} which did not solve the issue, even though setting that variable before running the script does work. Likewise, trying to run the export command from within the script had no effect.

Is there a better way to correct this?

答案1

得分: 2

听起来你的环境变量需要在加载PerlMagick之前进行定义。如果是这样,有两种方法可以在Perl中实现。

你可以在加载PerlMagick之前,在一个BEGIN块中设置环境变量:

BEGIN { $ENV{MAGICK_THREAD_LIMIT} = 1 }
use Image::Magick;

BEGIN块强制该赋值在编译时尽快发生,因此在use之前发生。如果你这样做,请不要BEGIN后加冒号。如果加了冒号,它仍然是有效的Perl,但现在它只是一个带标签的块,在运行时执行。

或者,你可以在运行时,在加载PerlMagick之前也在运行时设置环境变量:

$ENV{MAGICK_THREAD_LIMIT} = 1;
require Image::Magick;

require在运行时加载模块,而不是在编译时,因此在赋值执行之后发生。但是它不会将任何符号导入你的命名空间。这在你的情况下可能不是问题,因为Image::Magick有一个面向对象的接口。

如果你只是这样做:

$ENV{MAGICK_THREAD_LIMIT} = 1;
use Image::Magick;

use发生在编译时,因此赋值语句执行之前发生,即使use出现在脚本中赋值之后也是如此。

英文:

It sounds like your environment variable needs to be defined before PerlMagick is loaded. If this is true, there are two ways to do this inside Perl.

You can set the environment variable in a BEGIN block before loading PerlMagick:

BEGIN { $ENV{MAGICK_THREAD_LIMIT} = 1 }
use Image::Magick;

The BEGIN block forces the assignment to occur as soon as it is compiled, and therefore before the use. If you do this, do not put a colon after the BEGIN. If you do, it is still valid Perl, but now it is simply a labelled block, which is executed at run time.

Or, you can set the environment variable at run time before loading PerlMagick also at run time:

$ENV{MAGICK_THREAD_LIMIT} = 1;
require Image::Magick;

The require loads the module at run time rather than compile time, and therefore after the assignment gets executed. But it does not import any symbols into your name space. This is probably not a problem in your case, since Image::Magick has an O-O interface.

If you simply do

$ENV{MAGICK_THREAD_LIMIT} = 1;
use Image::Magick;

The use occurs at compile time, and therefore before the assignment statement is executed, even though the use appears after the assignment in the script.

答案2

得分: 1

我会将其设置在我的登录脚本中,例如 ~/.bash_profile,或者在系统范围的登录脚本中,适用于所有用户,例如 /etc/profile

export MAGICK_THREAD_LIMIT=1

另一种选择可能是编辑系统范围的 ImageMagick resources.xml。您可以使用以下命令找到它:

magick identify -list configure | grep -i xml

它将位于与 policy.xml 相同的目录中。

更多想法参见这里这里


最困难的选择将是从源代码重新构建 ImageMagick。在这种情况下,您需要执行以下操作:

./configure --disable-openmp ...
make -j
sudo make install
sudo ldconfig
英文:

I would set it in my login script, e.g. ~/.bash_profile, or in the system-wide login script for all users, e.g. /etc/profile:

export MAGICK_THREAD_LIMIT=1

Another option might be to edit the system-wide ImageMagick resources.xml. You can find this using:

magick identify -list configure | grep -i xml

and it will be in the same directory as policy.xml.

Extra ideas here and here.


The hardest option would be to rebuild ImageMagick from source. In that case, you'd need:

./configure --disable-openmp ...
make -j
sudo make install
sudo ldconfig

huangapple
  • 本文由 发表于 2023年6月2日 12:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定