Multi-processing using PHP

There are situations when we need to make our one-process task into a multi-process task so that we can take advantage of the operating system’s multitasking capabilities.

The basic conceptual difference between multiprocessing and multithreading– A process, is a unique instance of a program with its own memory space, own process ID number etc. Whereas a thread can be thought of as a virtual process, it does not have its own process ID, does not have its own memory space, but is still able to take advantage of multitasking.

I do agree that PHP is not that good for parallel computing, and PHP is not designed to do that actually. We can do better job using some other languages or technologies.

But the basic point is- there is a simple way to use PHP to handle multiprocessing at the basic level; In Unix, forking a process is quite fast and easier to handle, and PHP is able to fork processes at the run time, hence it can be used at the basic level at least.

A very simple use case of the same-

$ time php ImageResizerSingleProcess.php

real 0m6.233s
user 0m5.884s
sys 0m0.336s

$ time php ImageResizerMultiProcess.php

real 0m3.109s
user 0m10.636s
sys 0m1.172s

Interesting, see the difference.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s