rik447@hot.ee (Tanel) wrote in message news:<9957109e.0401200434.50dafd6d@posting.google. com>...[color=blue]
>
mcompengr@earthlink.net (martin) wrote in message >[color=green]
> > Are you sure it's blocking and not just looping once? I get
> > do_smth_else() being called once, but also no "Hello World" and
> > with fgets() returning "false" the first time, as expected. It
> > works for me without the "if ($line == false) break;" line and
> > with "while(!feof($process))" instead of "while(true)"[/color]
>
> Notice the three = in ($line === false).[/color]
Same difference. "false" is the empty string. fgets() returns
an empty string when reading from a non-blocking file-descriptor
which has nothing to read. "if ($line === false) break;" will
surely break out of your loop when $line comes from such a read,
as it surely must for at least the first read. Fgets() will not
wait for anything when reading from a non-blocking file-descriptor.
If, as you said, you want to "read a result of the first script
(that takes some time to run) from the second script so that the
first script doesn't block the second", and meanwhile be doing
something else, this will do that for you until the first script
is finished. (your example code modified):
$process = popen("c:\php\php -f test.php", 'r');
stream_set_blocking($process, false);
do {
$line = fgets($process);
do_something_with_line($line);
do_something_else();
} while(!feof($process));
pclose($process);
[color=blue]
> Here is maybe a better
> example to show what I mean. The first fgets waits until the test.php
> has finished, and second fgets has nothing. Desired behavior should be
> 1st fgets:Hello, 2nd fgets World!
>
> $process = popen("c:\php\php -f test.php", 'r');
> stream_set_blocking($process, false);
> print "\n 1st fgets:".fgets($process);
> sleep(3);
> print "\n 2nd fgets:".fgets($process);
> pclose($process);[/color]