开发者

Flex: Output of a C file in real time via PHP+Flex?

I have a C executable (named myprogram). When I run it by ./myprogram I get some output statements on the standard output of my Linux shell.

I used run.php to get access to whatever is being printed to the standard output:

#run.php
<?php
$output = shell_exec('./myprogram');
echo $output;
?>

Then I called an HTTPService having id="service" to access this run.php file and I also set the text attribute of a TextArea to {service.lastResult}. When I run my Flex app, the interface kind of halts for a moment and then the TextArea displays all开发者_JS百科 the 20 lines at once.

But if program is run from Linux shell they appear one by one.

Is there a way by which I can display a line as soon as it gets printed to standard output? This will then make the TextArea show the output in real time.

Or is Flex unable to do this? Rather how can a PHP program achieve such effect i.e. showing, whatever is being printed on standard output, concurrently in the browser in real time?

Update: Let's make the question little more specific and/or simple.

myprogram outputs 20 lines each after some microseconds. I want only the first line of standard output to appear in TextArea of Flex or in the browser (but in real time) while the program may then continue running the program.

Doesn't look like simple :P


HTTP doesn't work that way. You can simulate it with comet. In Java You can use BlazeDS for pushing data from server to flash client.


Can you modify the source of the C program? The reason for this behavior might be that he standard output in C is line buffered, but only if the program runs in a terminal. It will buffer larger chunks if (it thinks that) standard output is to a file or pipe. Therefore, your C program perhaps doesn't send any data until the end. You can add calls to fflush to force printing of the data:

#include <unistd.h>
#include <stdio.h>

int main(void) {
    printf("Hi!\n");
    fflush(stdout); /* Add this! */
    sleep(3);
    printf("Ho!\n");    
    return 0;
}


I have not tested it, but you might want to take a look at the passthru() function.

Edit: This will probably not work directly because of your indirection via {service.lastResult}. I'm not familiar with flex, but I assume that this will wait for the last result being complete before getting fired. So you'd have to find an alternative that allows you to grab the result of the service call while it is still processing - I do not know if this is possible.

The main point in using passthru() is that it will send the output of the invoked command directly, giving you at least a chance to grab intermediate results. exec(), system() and shell_exec() on the other hand will not send anything at all before completing, so there can be no workaround to begin with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜