views:

292

answers:

3

I am converting a PDF with PDF2SWF and Indexing with XPDF.. with exec.. only this requires the execution time to be really high.

Is it possible to run it as background process and then launch a script when it is done converting?

+1  A: 

in general, php does not implement threads.

But there is an ZF-class which may be suitable for you:

http://framework.zend.com/manual/en/zendx.console.process.unix.overview.html

ZendX_Console_Process_Unix allows developers to spawn an object as a new process, and so do multiple tasks in parallel on console environments. Through its specific nature, it is only working on nix based systems like Linux, Solaris, Mac/OSx and such. Additionally, the shmop_, pcntl_* and posix_* modules are required for this component to run. If one of the requirements is not met, it will throw an exception after instantiating the component.

suitable example:

class MyProcess extends ZendX_Console_Process_Unix
{
    protected function _run()
    {
        // doing pdf and flash stuff
    }
}

$process1 = new MyProcess();
$process1->start();

while ($process1->isRunning()) {

    sleep(1);

}

echo 'Process completed';

.

henchman
please provide feedback for downvote.
henchman
Sorry, it was an accident. I've marked it up now.
Cetra
Thanks this looks really nice but would be nice if had windows support too..
Chris
Just tried it and it seems this component always hangs... isRunning is always true even after the process completes.
Chris
A: 

If you are running it from the command line, you can fork a php process using pcntl_fork

There are also daemon classes that would do the same trick:

http://pear.php.net/package/System_Daemon

$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {

     //We are the parent, exit
exit();

} else {

// We are the child, do something interesting then call the script at the end.

}
Cetra
A: 

Try using popen() instead of exec().

This hack will work on any standard PHP installation, even on Windows, no additional libraries required. Yo can't really control all aspects of the processes you spawn this way, but sometimes this is enough:

$p1 = popen("/bin/bash ./some_shell_script.sh argument_1","r");
$p2 = popen("/bin/bash ./some_other_shell_script.sh argument_2","r");
$p2 = popen("/bin/bash ./yet_other_shell_script.sh argument_3","r");

The three spawned shell scripts will run simultaneously, and as long as you don't do a pclose($p1) (or $p2 or $p3) or try to read from any of these pipes, they will not block your PHP execution.

When you're done with your other stuff (the one that you are doing with your PHP script) you can call pclose() on the pipes, and that will pause your script execution until the process you are pclosing finishes. Then your script can do something else.

Note that your PHP will not conclude or die() until those scripts have finished. Reaching the end of the script or calling die() will make it wait.

Sebastián Grignoli
This does not seem to work in windows for me ?
Chris
Actually I see now that it does work.. unless the PHP I exectute contains the exec X_x Then it does not work and nothing in my apache logs... If I do something like fwrite it does work.
Chris