How to handle heavy calculations in the background in Drupal

Say you have a data warehouse or data mart in your Drupal installation and you have to make a quite heavy calculation on a node save, but you don't want the users to wait for too long. Then you have to add the process to the background.

One way of doing it is to set a flag and have a function called by hook_cron doing the job. But you might not want to wait for cron to run and you don't want to hammer the site with cron.

There is an awesome module called Background Process (http://drupal.org/project/background_process). It lets you add any function call and arguments and just run it in the background while your user don't have to wait for it.

<?php
//Here is a simple call to watchdog, see the result in /admin/reports/dblog
//The result will show that msg2 is executed before msg1
background_process_start('watchdog', "test", "msg1");
watchdog("test", "msg2");


//I can replace my function call...
my_heavy_function($node);

//...with this to move on faster in UI
background_process_start('my_heavy_function', $node);
?>

I do have some problems running it straight out of the box on Windows/WampServer, but on Linux it works perfect.

Knowledge keywords: