Home > Archive > PHP Language > July 2006 > pcntl_fork apache and system
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
pcntl_fork apache and system
|
|
|
| Hello,
I'm trying to run a system call('ls' in this simple case) that is taking
quite a long. I would like to fork the system call into a child process
and waits display the output of the call while it is running.
Here is a piece of code:
$pid = pcntl_fork();
for ($i = 1; $i <= 5; ++$i) {
$pid = pcntl_fork();
if (!$pid) {
$query = "ls /tmp";
system ($query,$ret_val);
print "In child $i\n";
exit;
}
}
How can i make this script dynamic in my web page. Meaning how can i
print each loop while the child process is running in apache and
redirect to STDOUT in the webpage???
| |
|
| "David" <david@nospam.com> wrote in message news:44a935e9$0$9020$626a54ce@news.free.fr...
> How can i make this script dynamic in my web page. Meaning how can i print each loop
> while the child process is running in apache and redirect to STDOUT in the webpage???
The methods flush and ob_flush.
-Lost
| |
|
| Well i did try both but doesn't seem to do what i want:
This does only display the first line that has been found, the rest is
not sent to the browser..!!!
$query ="find ./ -iname '*.txt' ";
echo "<b>Running query:<br>$query</B><br><br>";
ob_start();
system($query,$return_val);
ob_flush();
flush();
Any idea ???
-Lost a écrit :
> "David" <david@nospam.com> wrote in message news:44a935e9$0$9020$626a54ce@news.free.fr...
>
>
> The methods flush and ob_flush.
>
> -Lost
>
>
| |
|
| "David" <david@nospam.com> wrote in message news:44a93ca2$0$21817$626a54ce@news.free.fr...
> Well i did try both but doesn't seem to do what i want:
>
> This does only display the first line that has been found, the rest is not sent to the
> browser..!!!
>
> $query ="find ./ -iname '*.txt' ";
> echo "<b>Running query:<br>$query</B><br><br>";
> ob_start();
> system($query,$return_val);
> ob_flush();
> flush();
>
> Any idea ???
None, sorry! I am not on a *nix machine right now, so I cannot even test any code.
(Obviously, the Windows version tells me pcntl_fork is not even a valid function.)
-Lost
| |
|
| Ok no problem,
Let me know if you figure out..!!!
-Lost a écrit :
> "David" <david@nospam.com> wrote in message news:44a93ca2$0$21817$626a54ce@news.free.fr...
>
>
> None, sorry! I am not on a *nix machine right now, so I cannot even test any code.
> (Obviously, the Windows version tells me pcntl_fork is not even a valid function.)
>
> -Lost
>
>
| |
| Colin McKinnon 2006-07-03, 6:57 pm |
| David wrote:
> Here is a piece of code:
>
> $pid = pcntl_fork();
<snip>
>
> How can i make this script dynamic in my web page. Meaning how can i
> print each loop while the child process is running in apache and
> redirect to STDOUT in the webpage???
I can't help thinking that forking is the wrong place to start from - as
you've realised you suddenly get away from the simple I/O model.
The next problem is that (IIRC) ls first reads in the directory then
generates output - so the last directory entry will be ready pretty soon
after the first.
Another consideration is that if 'ls' is taking a long time, you've got
admin problems you need to solve.
-Lost's suggestion of using flush is a good one (don't bother forking - just
use popen) - but depending on the furniture you put about it, your browser
may have problems rendering the incomplete page - if you are using a fancy
layout you might consider embedding the output in an iframe, or using
javascript document.writes to insert it afterwards.
HTH
C.
| |
|
| Thanks, that's exactly what i have done. Using the popen function did
the work. Now the output is passed to the web browser immedialty after
launching.
function show_buffer($query)
{
echo "<pre>";
$handle = popen($query, 'r');
while(!feof($handle)) {
$buffer = fgets($handle);
echo "$buffer";
ob_flush();
flush();
}
pclose($handle);
echo "</pre>";
}
$query = "ls /tmp/*.txt";
show_buffer($query);
Colin McKinnon a écrit :
> David wrote:
>
> <snip>
>
> I can't help thinking that forking is the wrong place to start from - as
> you've realised you suddenly get away from the simple I/O model.
>
> The next problem is that (IIRC) ls first reads in the directory then
> generates output - so the last directory entry will be ready pretty soon
> after the first.
>
> Another consideration is that if 'ls' is taking a long time, you've got
> admin problems you need to solve.
>
> -Lost's suggestion of using flush is a good one (don't bother forking - just
> use popen) - but depending on the furniture you put about it, your browser
> may have problems rendering the incomplete page - if you are using a fancy
> layout you might consider embedding the output in an iframe, or using
> javascript document.writes to insert it afterwards.
>
> HTH
>
> C.
|
|
|
|
|