amolkad wrote:sorry i forgot to mentioned that we have some session and global variable which we need to use in those included files.
It's not a problem as you may store global variable to session and pass session id to child script. E.g.:
Code: Select all
<?php
$a = 'blah-blah-blah';
$b = 'something';
session_start();
function save_globals($which) {
unset($_SESSIONї'GLOBALS']);
foreach($GLOBALS as $key => $val)
if( in_array($key, $which) )
$_SESSIONї'GLOBALS']ї$key] = $val;
}
/*
* start multiple tasks in background
*
* each task will get sessionID as its last parameter
* to access session variables of the parent script you would use it like:
* //....
* if($argv) session_id( end($argv) );
* session_start();
* //....
*
* selected variables from the global scope of the
* parent script will be available as
* $_SESSIONї'GLOBALS']ї$variable_name]
* e.g. $_SESSIONї'GLOBALS']ї'a'], $_SESSIONї'GLOBALS']ї'b']
*
* you might want to extract($_SESSIONї'GLOBALS']) for your
* convenience, e.g.:
* //.....
* extract($_SESSIONї'GLOBALS']);
* do_something_with($a); // '$a' is imported from the global scope of the
* // parent script
* //.....
*/
function start_background_tasks($commands, $vars) {
save_globals($vars);
session_write_close();
$pids = array();
foreach($commands as $key => $command) {
ob_start();
$pidsї$key] = system($command . ' ' . session_id() . ' >/dev/null & echo $!');
ob_end_clean();
}
return $pids;
}
$commands = array(
'php script1.php',
'php script2.php',
//...and so forth
);
$pids = start_background_tasks($commands, array('a', 'b'));
echo '<pre>Started tasks in background. Process IDs are:';
print_r($pids);
echo '</pre>';
//.......
?>
Code: Select all
<?php
set_time_limit(0);
if( $argv ) session_id( end($argv) );
session_start();
extract($_SESSIONї'GLOBALS']);
session_write_close();
sleep(rand(30,60));
$fp = fopen('/tmp/1.log', 'a');
fputs($fp, $a);
fclose($fp);
?>
Code: Select all
<?php
set_time_limit(0);
if( $argv ) session_id( end($argv) );
session_start();
extract($_SESSIONї'GLOBALS']);
session_write_close();
sleep(rand(30,60));
$fp = fopen('/tmp/2.log', 'a');
fputs($fp, $b);
fclose($fp);
?>
as you can see, main.php starts two child processes and immidiately exits after then. Each child process then sleeps for a while, opens a file and writes the content of a variable imported from the global scope of main.php to its own file. Child processes in this example have full access to the session variables of the main script, as well as read-only access to the selected variables from the global context of their parent.