exec()在PHP中运行缓慢-相同的命令以交互方式运行得更快 [英] exec() runs slow in PHP - same command runs much faster interactively

查看:424
本文介绍了exec()在PHP中运行缓慢-相同的命令以交互方式运行得更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么exec()函数这么慢并且在PHP的不同机器上无法预测时间?

Can anyone tell me why exec() function is so slow and time unpredictable on different machines in PHP?

基本上我有一些可执行文件,想通过PHP执行它:

Basically I have some executable file and want to execute it through PHP:

$command = '/usr/pathToComman/myCommand -someParameters';
exec($command);

问题是,它执行的时间要长得多(我的意思是说3-4,有时大约要长25倍),而执行该命令的时间要比从外壳程序执行的命令长得多.

The thing is that it executes much longer (by much I mean 3-4 sometimes like 25 times longer) then the same command from the shell.

除此之外,该命令在服务器上的PHP执行时间更长,因此功能更强大(更多的RAM和更多的GHz).

In additional to this the command is executed longer from the PHP on my server, which is stronger (more RAM and more GHz).

所以有两个问题:

  • 为什么?
  • 我该怎么办?

P.S.我需要执行此命令,因为我无法使用PHP做相同的事情

P.S. I need this execute, because I can not do the same thing with PHP

P.S.2回答@prodigitalson问题:它解决了微分方程,但是基本上不管它做什么,速度都慢得多.我记得以这种方式进行图像处理,而且速度也慢得多.

P.S.2 Answering @prodigitalson question: It solves the differential equation, but basically no matter what it does, the speed is just much slower. I remember doing some image processing this way and the speed was also much slower.

推荐答案

之所以变慢,可能是因为您的PHP服务器必须转到shell才能运行命令.因此,PHP服务器要做的就是创建一个新的shell,并在新的shell上调用命令.在大多数系统中,创建新的外壳意味着创建新的线程. 因此,所有这些因素共同导致更长的执行时间.

The problem why this is slower is probably because your PHP server has to go to the shell to run your command. So what the PHP server does then is create a new shell and call the command on the new shell. Creating a new shell means in most systems to create a new thread. So all those things together result in a longer execution time.

有时cgi脚本可以解决这些问题,但是我不确定它是否对您有帮助(因为我真的不知道您要在shell上做什么)

Sometimes a cgi script can help with those problems, but I'm not sure if it will help here (because I don't really know what you are trying to do on the shell)

这篇关于exec()在PHP中运行缓慢-相同的命令以交互方式运行得更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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