PHP exec()性能 [英] PHP exec() performance

查看:520
本文介绍了PHP exec()性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下PHP代码确实为我返回了大约3.5秒的运行时间(多次测量并取平均值):

The following PHP code does return me a runtime of about 3.5 seconds (measured multiple times and averaged):

$starttime = microtime(true);
exec('/usr/local/bin/convert 1.pdf -density 200 -quality 85% 1.jpg');
$endtime = microtime(true);
$time_taken = $endtime-$starttime;

当我在ssh终端上运行相同的命令时,运行时间减少到约0.6秒(使用命令行工具time测量).

When i run the same command over a ssh terminal, the runtime is reduced to about 0.6 seconds (measured with the command line tool time).

imagemagick库的版本为

The version of the imagemagick library is

Version: ImageMagick 6.7.0-10 2012-12-18 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP

造成这种时差的原因是什么?

What could be the reason for this time difference?

一个关于stackoverflow的类似问题的答案是,开销来自Web服务器必须启动线程/shell.这真的是原因吗?我认为线程是轻量级的,根本不需要花费很长时间就可以开始/终止线程.

One answer to a similar question here on stackoverflow was that the overhead comes from the Webserver having to start a thread/shell. Could this be really the reason? I thought threads are leightweight and don't take long at all to start/terminate.

在调用exec之前,我设置了imagemagick使用的线程数(因为这是/是OpenMP中的错误?,

Prior to calling exec i set the number of threads used by imagemagick (because this was/is a bug in OpenMP?, Reference) to 1 with exec('env MAGICK_THREAD_LIMIT=1');. The runtime from PHP does not change much, no matter what value i set for MAGICK_THREAD_LIMIT. Anyway there does not seem to be a bug in OpenMP on in this version because the runtime of the command line execution is ok.

任何有关如何改善上述命令的运行时间的建议将不胜感激.

Any suggestions of how i could improve the runtime of the above command would be greatly appreciated.

非常感谢您的帮助.

推荐答案

当您通过键盘或通过ssh登录Unix计算机时,将创建一个新的Shell实例.外壳通常是/bin/sh/bin/bash之类的东西.该外壳程序使您可以执行命令.

When you log in to a Unix machine, either at the keyboard, or over ssh, you create a new instance of a shell. The shell is usually something like /bin/sh or /bin/bash. The shell allows you to execute commands.

使用exec()时,它还会创建一个新的Shell实例.该实例将执行您发送给它的命令,然后退出.

When you use exec(), it also creates a new instance of a shell. That instance executes the commands you sent to it, and then exits.

创建新的Shell命令实例时,它具有自己的环境变量.因此,如果您这样做:

When you create a new instance of a shell command, it has it's own environment variables. So if you do this:

exec('env MAGICK_THREAD_LIMIT=1');
exec('/usr/local/bin/convert 1.pdf -density 200 -quality 85% 1.jpg');

然后创建两个外壳,第一个外壳中的设置永远不会到达第二个外壳.要将环境变量放入第二个shell中,您需要这样的内容:

Then you create two shells, and the setting in the first shell never gets to the second shell. To get the environment variable into in the second shell, you need something like this:

exec('env MAGICK_THREAD_LIMIT=1; /usr/local/bin/convert 1.pdf -density 200 -quality 85% 1.jpg');

现在,如果您认为外壳本身可能是问题所在,因为制作外壳所需的时间太长,请使用您知道几乎不需要时间的东西对其进行测试:

Now, if you think that the shell itself may be the problem, because it takes too long to make a shell, test it with something that you know takes almost no time:

$starttime = microtime(true);
exec('echo hi');
$endtime = microtime(true);
$time_taken = $endtime-$starttime;

到那时,您知道可以尝试并找到一些使Shell实例化更快的方法.

At that point you know to try and find some way to make the shell instantiate faster.

希望这会有所帮助!

这篇关于PHP exec()性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆