I'm trying to run ImageMagick without touching the filesystem: image data is read from memory/the network, and written back to a socket as a blob.
我试图运行ImageMagick而不触及文件系统:图像数据从内存/网络中读取,并以blob形式写回套接字。
However, ImageMagick continually tries to write temp files, which either fill up my test system due to aborted/failed tests, or cause problems on systems with extremely slow disks: it's a weird constraint, but many of my conversion hosts are embedded-like systems with block devices that are extremely slow to respond to any operations, even stat()
s.
然而,ImageMagick不断试图写入临时文件,要么填满我的测试系统由于流产/失败的测试,用极慢的磁盘或造成问题在系统:这是一个奇怪的约束,但我的许多转换主机embedded-like系统与块设备是极其缓慢的回应任何操作,甚至stat()。
Questions:
问题:
-
Is there a way to configure ImageMagick to not touch the disk during image processing?Assume that all required modules that ImageMagick will use have already been loaded, and that none of the ImageMagick functionality that farms processing out to subprocesses that talk to the filesystem will be used.
是否有一种方法可以配置ImageMagick在图像处理过程中不触及磁盘?假设ImageMagick将使用的所有必需的模块都已经加载,并且不会使用处理与文件系统对话的子进程的ImageMagick功能。
-
What are the side effects of doing this? I'm OK with processing that won't fit in memory failing, rather than falling back to the disks.
这样做的副作用是什么?我不介意处理不适合内存失败的问题,而不是回到磁盘上。
I'm converting using the C++ or Perl ImageMagick APIs, not the convert
utility. If another binding has support for this, though, I'm fine switching platforms.
我使用c++或Perl ImageMagick api进行转换,而不是使用转换实用程序。如果另一个绑定支持此功能,那么我可以切换平台。
What I've Tried
我已经试过
- I've set the
MAGICK_TEMPORARY_PATH
to various places, including/dev/null
on POSIX./dev/null
sometimes seems to work, but my target systems aren't POSIX, so I'm not sure if I can count on it. Rather than fooling the temp file management system, I'd prefer something that I can trust to disable the need for temp files in the first place. - 我已经将MAGICK_TEMPORARY_PATH设置为不同的位置,包括POSIX上的/dev/null。/dev/null有时似乎可以工作,但是我的目标系统不是POSIX,所以我不确定我是否可以依赖它。与其欺骗临时文件管理系统,我更希望能够信任的东西首先禁用临时文件的需要。
- I've used the
registry:temporary-path
option, with similar results. - 我使用了注册表:临时路径选项,结果类似。
- Setting the various temporary path options to nonexistent/unusable locations (e.g.
/dev/null
or a non-writable location) often seems to result in temporary files being created in the directory from which my program is launched. If that directory is non-writable, I have seen temp files get created in the system/tmp
directory even if the code was told not to use it. These seem to happen in exceptional cases (e.g. segfaulting/OOMing/kill -9
'd situations), so I gather that files may be created in these places normally, but are usually cleaned up. - 将各种临时路径选项设置为不存在/不可用的位置(例如/dev/null或不可写位置)通常会导致在启动程序的目录中创建临时文件。如果该目录是不可写的,我曾看到在system /tmp目录中创建临时文件,即使代码被告知不要使用它。这些似乎是在特殊情况下发生的(例如分段错误/OOMing/kill - 9d情况),所以我认为这些文件通常可以在这些地方创建,但通常会被清理。
1 个解决方案
#1
1
The trick is to set MAGICK_DISK_LIMIT=0
and MAGICK_AREA_LIMIT
/MAGICK_AREA_LIMIT
to a value larger than the maximum memory required by the program.
诀窍是将MAGICK_DISK_LIMIT=0和MAGICK_AREA_LIMIT/MAGICK_AREA_LIMIT设置为大于程序所需的最大内存的值。
#!/usr/bin/perl
BEGIN {
$ENV{'MAGICK_DISK_LIMIT' }='0';
$ENV{'MAGICK_AREA_LIMIT' }='1Mb';
$ENV{'MAGICK_MEMORY_LIMIT'}='1Mb';
}
use Image::Magick;
my $img = Image::Magick->new();
my $err = $img->Read('in.png');
die $err if $err;
If ImageMagick exceeds this memory limit it will die with a message like:
如果ImageMagick超过这个内存限制,它将会死于如下消息:
Exception 450: Memory allocation failed `in.png' @ error/png.c/MagickPNGErrorHandler/1645 at ./imagemem.pl line 11.
You can view ImageMagick's default limits by running identify -list resource
您可以通过运行标识列表资源来查看ImageMagick的默认限制
#1
1
The trick is to set MAGICK_DISK_LIMIT=0
and MAGICK_AREA_LIMIT
/MAGICK_AREA_LIMIT
to a value larger than the maximum memory required by the program.
诀窍是将MAGICK_DISK_LIMIT=0和MAGICK_AREA_LIMIT/MAGICK_AREA_LIMIT设置为大于程序所需的最大内存的值。
#!/usr/bin/perl
BEGIN {
$ENV{'MAGICK_DISK_LIMIT' }='0';
$ENV{'MAGICK_AREA_LIMIT' }='1Mb';
$ENV{'MAGICK_MEMORY_LIMIT'}='1Mb';
}
use Image::Magick;
my $img = Image::Magick->new();
my $err = $img->Read('in.png');
die $err if $err;
If ImageMagick exceeds this memory limit it will die with a message like:
如果ImageMagick超过这个内存限制,它将会死于如下消息:
Exception 450: Memory allocation failed `in.png' @ error/png.c/MagickPNGErrorHandler/1645 at ./imagemem.pl line 11.
You can view ImageMagick's default limits by running identify -list resource
您可以通过运行标识列表资源来查看ImageMagick的默认限制