PHP CodeIgniter: How to run processes in the background without getting my whole website stuck

You can solve this issue in two ways.

  1. Create a cronjob to do the time consuming task in the main method you just need to add an enrty to a job table to the post processing after sending the response. So reponse will not wait until all the processing is completed. Also you can schedule the cronjob time as you need depend on the urgency of the post processing and server load.
  2. You can use CodeIgniter hook function to do the processing after sending the response to the caller. The hook method will be “post_system” Read more about it at

What you’re really after is a queue or a job system. There’s one script running all the time, waiting for something to do. Once your original PHP script runs, it just adds a job to the list, and it can continue it’s process as normal.

I found a solution that works perfect for me because i don’t expect any result value. If notification can’t be send…i log it in my database.

This is the function that i use to send “async” request (yes, This is not an asynchronous request, but it works how i’m looking for)

function curl_post_async($url, $params)
    $post_string = http_build_query($params);

    $fp = fsockopen($parts['host'],
        $errno, $errstr, 30);

        //Perform whatever logging you want to have happen b/c this call failed!    
    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
(Visited 86 times, 1 visits today)



Leave a Reply