By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

forking multiple processes at a time

P: n/a
i have a php script that needs to execute an external php script (call this
script 2) when the user clicks a link, however I don't want the user to have
to wait until script 2 finishes executing before he sees the output of
script one... i know I can use the exec() command to do this, and use the &
to run the command in the background....

but say i want to run 100 instances of script 2 when the user clicks the
link (with different arguments)... i dont want to have all 100 processes
running at the same time... what I would like to do is run 10 or so at once,
and once those 10 are completed, run another 10...

how would i accomplish this?
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
SomeGei wrote:
i have a php script that needs to execute an external php script (call this
script 2) when the user clicks a link, however I don't want the user to have
to wait until script 2 finishes executing before he sees the output of
script one... i know I can use the exec() command to do this, and use the &
to run the command in the background....

but say i want to run 100 instances of script 2 when the user clicks the
link (with different arguments)... i dont want to have all 100 processes
running at the same time... what I would like to do is run 10 or so at once,
and once those 10 are completed, run another 10...

how would i accomplish this?


Could the last process of each batch launch the next 10 processes?

Another approach is using cron and having script 1 store in a file the
arguments for the 100 instances of script 2. cron would run a "launcher"
every minute, and this launcher would execute a copy of script 2 for
each set of arguments in the file.

Jul 17 '05 #2

P: n/a
SomeGei wrote:
i dont want to have all 100 processes running at the same time...
what I would like to do is run 10 or so at once,
and once those 10 are completed, run another 10...

how would i accomplish this?


Check out popen() and related functions
http://www.php.net/popen

<?php // basic code with no error-checking
for ($i=0; $i<10; ++$i) {

/* open ten processes */
for ($j=0; $j<10; ++$j) {
$pipe[$j] = popen('script2.php', 'w');
}

/* wait for them to finish */
for ($j=0; $j<10; ++$j) {
pclose($pipe[$j]);
}
}
?>
--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #3

P: n/a

"SomeGei" <so**@gei.com> wrote in message
news:qJ********************@giganews.com...
i have a php script that needs to execute an external php script (call this script 2) when the user clicks a link, however I don't want the user to have to wait until script 2 finishes executing before he sees the output of
script one... i know I can use the exec() command to do this, and use the & to run the command in the background....

but say i want to run 100 instances of script 2 when the user clicks the
link (with different arguments)... i dont want to have all 100 processes
running at the same time... what I would like to do is run 10 or so at once, and once those 10 are completed, run another 10...

how would i accomplish this?


Have each instance of the program spawn a copy of itself when it finishes.
Jul 17 '05 #4

P: n/a
SomeGei wrote:
i have a php script that needs to execute an external php script (call this script 2) when the user clicks a link, however I don't want the user to have to wait until script 2 finishes executing before he sees the output of script one... i know I can use the exec() command to do this, and use the & to run the command in the background....

<snip>

http://in.php.net/system
<quote>
Note: If you start a program using this function and want to leave it
running in the background, you have to make sure that the output of
that program is redirected to a file or some other output stream or
else PHP will hang until the execution of the program ends.
</quote>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.