Discussion:
Echo from stream_select as output is generated by child process?
Mike Kilmer
2014-06-05 22:18:01 UTC
Permalink
Hi all. This is my first post here.

I'm using proc_open to call a python script.

The (python) child process takes about a minute to run, and when run from the command line it prints results as they are completed.

With the php script, it just prints them all when it's complete.

Is there a way to return the python output to the browser in real time?

Possibly using more than one stream_select with varying time_outs?

The full script is posted on StackOverflow, and what I think are the relevant segments are below:

$proc = proc_open ( $child_process , $description , $pipes, glitch_player_DIR, $env );

// set all streams to non blockin mode
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);

foreach(array(1, 2) as $desc) {
// check stdout for data
$read = array($pipes[$desc]);
$write = NULL;
$except = NULL;
$tv = 0;
$utv = 50000;

$n = stream_select($read, $write, $except, $tv, $utv);
if($n > 0) {
do {
$data = fread($pipes[$desc], 8092);
if (is_string($data) && ($data != ""))
echo $data ."<br/>";
array_push($glitch_response, $data);
} while (strlen($data) > 0);
}
}

Thanks for your feedback.

Mike Kilmer
mZoo.org
Aziz Saleh
2014-06-05 22:25:18 UTC
Permalink
Post by Mike Kilmer
Hi all. This is my first post here.
I'm using proc_open to call a python script.
The (python) child process takes about a minute to run, and when run from
the command line it prints results as they are completed.
With the php script, it just prints them all when it's complete.
Is there a way to return the python output to the browser in real time?
Possibly using more than one stream_select with varying time_outs?
The full script is posted on StackOverflow, and what I think are the
$proc = proc_open ( $child_process , $description , $pipes,
glitch_player_DIR, $env );
// set all streams to non blockin mode
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
foreach(array(1, 2) as $desc) {
// check stdout for data
$read = array($pipes[$desc]);
$write = NULL;
$except = NULL;
$tv = 0;
$utv = 50000;
$n = stream_select($read, $write, $except, $tv, $utv);
if($n > 0) {
do {
$data = fread($pipes[$desc], 8092);
if (is_string($data) && ($data != ""))
echo $data ."<br/>";
array_push($glitch_response, $data);
} while (strlen($data) > 0);
}
}
Thanks for your feedback.
Mike Kilmer
mZoo.org
What do you have for your descriptor array (second param on proc_open)?
Mike Kilmer
2014-06-05 22:52:24 UTC
Permalink
just the basic:

$description = array (
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
Post by Aziz Saleh
Post by Mike Kilmer
Hi all. This is my first post here.
I'm using proc_open to call a python script.
The (python) child process takes about a minute to run, and when run from
the command line it prints results as they are completed.
With the php script, it just prints them all when it's complete.
Is there a way to return the python output to the browser in real time?
Possibly using more than one stream_select with varying time_outs?
The full script is posted on StackOverflow, and what I think are the
$proc = proc_open ( $child_process , $description , $pipes,
glitch_player_DIR, $env );
// set all streams to non blockin mode
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
foreach(array(1, 2) as $desc) {
// check stdout for data
$read = array($pipes[$desc]);
$write = NULL;
$except = NULL;
$tv = 0;
$utv = 50000;
$n = stream_select($read, $write, $except, $tv, $utv);
if($n > 0) {
do {
$data = fread($pipes[$desc], 8092);
if (is_string($data) && ($data != ""))
echo $data ."<br/>";
array_push($glitch_response, $data);
} while (strlen($data) > 0);
}
}
Thanks for your feedback.
Mike Kilmer
mZoo.org
What do you have for your descriptor array (second param on proc_open)?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Maciek Sokolewicz
2014-06-05 23:06:40 UTC
Permalink
Post by Mike Kilmer
Hi all. This is my first post here.
I'm using proc_open to call a python script.
The (python) child process takes about a minute to run, and when run from the command line it prints results as they are completed.
With the php script, it just prints them all when it's complete.
Is there a way to return the python output to the browser in real time?
This actually sounds like the issue is not in your actual PHP script
itself (or at least not in the proc-part) but rather in the fact that
your server waits until the script has finished and then sends the
entire output in one go to the client.

So... there are 2 things you should do:
1. make sure you're not buffering the output (ie. call ob_end_flush() at
the start of your script to make sure output-buffering is turned off)

2. Configure your server to send continuous chunked output. How heavily
depends on your server. You could for example opt to use
chunked-encoding. This thread on stackoverflow may help:
http://stackoverflow.com/questions/4809774/transfer-encoding-chunked-header-in-php

- Tul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Becker
2014-06-05 23:13:11 UTC
Permalink
Post by Mike Kilmer
I'm using proc_open to call a python script.
The (python) child process takes about a minute to run, and when run
from the command line it prints results as they are completed.
With the php script, it just prints them all when it's complete.
Is there a way to return the python output to the browser in real time?
Have you tried running the PHP script from the command line? It might
be possible that the PHP script echoes the output of the Python script
"immediately", but that PHP's output is buffered by the webserver or
somehow "delayed" by the browser.
Post by Mike Kilmer
The full script is posted on StackOverflow, and what I think are the
It might be useful to post a link to the StackOverflow topic.
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Mike Kilmer
2014-06-05 23:25:50 UTC
Permalink
Great feedback from all of you so far!

In the meantime, here is the SO link: http://stackoverflow.com/questions/23945328/multiple-stream-select-with-proc-open-receive-responses-from-slow-process
Post by Christoph Becker
Post by Mike Kilmer
I'm using proc_open to call a python script.
The (python) child process takes about a minute to run, and when run
from the command line it prints results as they are completed.
With the php script, it just prints them all when it's complete.
Is there a way to return the python output to the browser in real time?
Have you tried running the PHP script from the command line? It might
be possible that the PHP script echoes the output of the Python script
"immediately", but that PHP's output is buffered by the webserver or
somehow "delayed" by the browser.
Post by Mike Kilmer
The full script is posted on StackOverflow, and what I think are the
It might be useful to post a link to the StackOverflow topic.
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...