5

I understand from PHP exec() performance that running exec() creates an overhead but in large programs or websites, wouldn't it be beneficial to have parts of the backend written in another language, and have PHP call the program using exec?

For instance, I wrote/ran a test with a large amounts of string manipulation - PHP took 2.3s while Java took .52 and C++ took .33. The speed difference is already obvious. The time could be sped up even more if I multithreaded the operation. I also found that parallelism can be achieved in with something like

exec("./largeoperation > mydir/$dirname.data &"); 
//or
exec('java Backend > /dev/null 2>&1 &');

With all these benefits, other than the need to write code in another language, I fail to see why I shouldn't relegate more parts of my backend to a faster program written in a different language. Also, I know of the existence of bridges like Working with Php-Java Bridge but I'm not sure if using that would be THAT much faster than simple exec(). Does anyone have any more details on exec()?

Community
  • 1
  • 1
wonton
  • 7,568
  • 9
  • 56
  • 93
  • 1
    You probably want to use a [message queue](http://en.wikipedia.org/wiki/Message_queue) and workers here instead of invoking programs via `exec`. I like [Gearman](http://gearman.org/). – Charles Jan 12 '13 at 02:54
  • Gearman is incredible. Thank you so much! – wonton Jan 12 '13 at 03:12
  • 1
    Another convert! Welcome to the cult. Lemme whip up an answer so we can close this one out. – Charles Jan 12 '13 at 03:35

1 Answers1

4

While you could farm work out to other programs using exec and friends, the thing you're trying to do is best accomplished using a message queue system. A well-designed message queue will let you write worker programs in any language you need. They're a great solution when you need to do something in another language, environment, or server.

There are lots of message queues, but I'm a big fan of Gearman. It was built by the same folks that brought us memcached. Take a peek at the PECL extension.

Charles
  • 50,943
  • 13
  • 104
  • 142