On Sun, Jun 26, 2011 at 7:42 PM, Tamara Temple <tamouse.li...@gmail.com>wrote:

> How do I launch a php script from another running php script
> asynchronously?
>

You can perform the long-running job in the same process that handles the
request by sending appropriate headers. We use this to run reports that take
ten minutes without making the user keep the browser tab open. Call this
method before starting the job:

    /**
     * Effectively causes PHP to keep running after flushing everything and
closing the client connection
     *
     * @param mixed $status JSON-encoded and sent to the client
     */
    public function flushAndCloseConnection($status) {
        // disable any apache or php gzipping
        if (function_exists('apache_setenv')) {
            apache_setenv('no-gzip', 1);
        }
        ini_set('zlib.output_compression', 0);
        // Tell the server and client we intend for it to disconnect
        ignore_user_abort(true);
        // Tell the client we are done!
        header('Connection: close');
        header('Content-type: application/json');
        $json = json_encode($status);
        header('Content-Length: ' . strlen($json));
        echo $json;
        flush();
    }

In our case we send a JSON status string ("ok") as well.

Another option is to fork the process using pcntl_fork() [1]. I haven't used
this in PHP before so I can't give an example.

Peace,
David

[1] http://www.php.net/manual/en/function.pcntl-fork.php

Reply via email to