Running php script after background process is complete?

I am converting PDF with PDF2SWF and indexing with XPDF .. with exec .. only this requires the runtime to be really high.

Is it possible to run it as a background process and then run the script when it is converted?

+4
source share
3 answers

in general, php does not implement threads.

But there is a ZF class that may come in handy:

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 therefore several tasks are performed in parallel in console environments. Due to their specific nature, they work only on nix-based systems such as Linux, Solaris, Mac / OSx, etc. In addition, shmop_, pcntl_ * and this require modules posix_ * components to run. If one of the requirements is not met, it will throw an exception after the component is instantiated.

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'; 

.

+2
source

Try using popen () instead of exec ().

This hack will work on any standard PHP installation, even on Windows; no additional libraries are required. Yo cannot really control all aspects of the processes that you create in 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 shell scripts generated will run simultaneously, and unless you do pclose ($ p1) (or $ p2 or $ p3) or try to read from any of these channels, they will not block your PHP execution.

When you finish with your other stuff (what you are doing with your PHP script), you can call pclose () in the pipes and this will pause your script until you run pclosing ends. Then your script can do something else.

Note that your PHP will not complete or die () until these scripts end. Reaching the end of the script or calling die () will keep it waiting.

0
source

If you use it from the command line, you can develop the php process using pcntl_fork

There are also daemon classes that would do the exact 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. } 
-1
source

Source: https://habr.com/ru/post/1303631/


All Articles