So you’ve just built a fantastic processing routine for your application. You’ve checked and double checked the integrity of user input, and you’re doing some serious processing. There’s only one problem: it’s too slow. There’s a simple solution: forking your processing script, and running the code as a background process asynchronously. It can email your user when it’s done: they’ll wait. In this tutorial, I’ll show you how to get started with background processes in PHP.
When we want some kind of background process running our PHP code, we have a simple challenge: we can’t leave it up to the client. On the web, people hit their stop button, their browsers lock up and they force quit, they even have the odd power outage. When we want to do complex processing on the server side, we need to detach from the client-server model of a web-served script. PHP can fork a script, but the parent script is still facing an unreliable client.
Our best option, then, is to launch a background process from the command line. There are three steps to this: starting the background script, passing it information to do its job, and checking when it’s finished.
Starting the background script bolt dvd download
On UNIX systems, we can easily call another PHP script via the shell, set it to run off in the background and send its output to /dev/null. Here’s a sample:
exec ("/usr/bin/php proc.php >/dev/null &");
Here, we call the
exec function; instructing it to run our local PHP binary – usually at /usr/bin/php or /usr/local/bin/php, check with your host/sysadmin. We pass it a single argument: the filename of the PHP script that handles our background process. We then send the output to /dev/null, effectively destroying it. The last character, the & symbol, is important – this tells the system to start this process in the background and to let us continue.
Talking to the background process
It’s all very well to start a background process, but now it needs to know what to do. We handle this through command line arguments. Let’s update the previous example:
exec ("/usr/bin/php proc.php --task=50 >/dev/null &");
Our proc.php file might look like this:
Running "php proc.php --task=50" on command line produces the following output:Handling process --task=50
From here, you can easily design your script to do what it needs to.
Monitoring the background process
There's just one last detail: how do we monitor the progress of our background process? It's best to use an independent datastore that can be relied upon, instead of communicating with running threads - your application's MySQL database would be perfect. You can create a "sessions" or "jobs" table with an ID column, a status column (started, in progress, finished - 0, 1, 2), and any other information you need. Before you start your background process, create a new entry in this table and check the insert ID, then pass it to the background process when you start it:exec ("/usr/bin/php proc.php --some_info=50 --job=1234 >/dev/null &");
Your proc.php file can then update the table row with the ID you supply it, and your front end applications can provide status updates to end users by checking up on all pending jobs for the current user - relatively straightforward business logic.
If you're going to be running many background processes, a job queue and an "always-on" script running on your server may also be an option. In this case, consider looking into pcntl_fork() and the program execution functions.