Wednesday, September 14, 2011

Celery task queue with PHP

Celery is a piece of software that helps you easily run time-consuming tasks. If you have ever used system() or exec() to create a big ZIP file or encode a video, you probably already know what the problem is. If you didn't - well, time-consuming tasks executed from within a PHP script (or any other web application) usually generate many problems, unresponsive user interface and timeouts being two major ones. There are many ways to overcome these issues, but face it - spending many hours and hundreds of lines of code just to create a ZIP file isn't especially effective.

The above code calls the command-line zip command with two arguments: path to a zip file and path of the files to be archived. "def" is Python for "function". (this is just an example, in real world you will probably want to validate paths and replace zip command with Python's zipfile module)

Now you will probably want to call that code from your PHP application. First schedule the task for execution:
$c = new Celery('localhost', 'myuser', 'mypass', 'myvhost');
$_SESSION['zip_result'] = $c->PostTask('tasks.create_zip', array('/home/user/file.zip', '/home/user/images/'));

Then you will want to somehow asynchronously display the result to user. You can use AJAX or some other technique to display the result of this script every second:
$result = $_SESSION['zip_result'];
if(!$result->isReady())
{
echo 'Please wait...';
}
else
{
echo '<a href="/file.zip">Ready!</a>';
}

No comments:

Post a Comment

About us

We are a small team of professional programmers and system administrators united by a common vision: faster websites make the world a better place.

Fast websites are a pleasure to browse. Services demanding less computational power are more friendly, both environmentally and economically. It's our passion to make them fast, reliable and efficient.