Murga-Projects Forums

Full Version: real time activity
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Got a question...
I'm trying to gather the output of a system command and display it in a browser object, however, if the command takes a while the window will not appear (with the browser object in it) until the command is complete.. even-though the command is producing output. Actually the output window will start to appear, depending on the command, but won't fully paint. For example doing a ping or traceroute and showing the output as it is produced. Is there a better/recommended way to do this:

function execute_cmd()
local fh = io.popen( cmd );
if ( fh ) then
while true do
local data = fh:read( "*l" );
if data == nil then break end
br_output:add( data );

I tried messing with this a couple of times, and didn't find an easy way to do it.

I suspect coroutines are an answer, but I'm afraid I can't help with that.

co-routines might help, but I suspect it'll still block.

Take a look at :


That effectively does exactly the same thing.

Basically instead of calling itself with "murgaLua_ExePath", you'd call your cmd with parameters.

To test I used the test_pid.lua file and replaced 'murgaLua_ExePath' with "/bin/ping" and placed the IP address into the first place of 'args'.. but it still blocks. I was trying to do something like '-c 4' for the args so it didn't block forever but didn't find a combination that it liked..

Am I trying this correctly?
I tried to bury the command a bit... '/bin/ping -c 4 | {while read line; do echo $line; done }' but couldn't figure out how to get it into the sys.spawn() call.. but did put it into a script and call it with the address as an arg and it does allow the timer to work. The output from the command/script is captured from what looks like a mixture of the fdi:read() call in the on_timer function and the fdi:read() call just beyond the sys.spawn() block,.. removing the one beyond sys.spawn() allows the one in on_timer() to handle all output (need to populate a widget). Now I guess I just need to kill the timer when all the data has been retrieved.. how do I do that?

Oh, and since I moved the command to a script and call it with an arg it doesn't really need the extra while loop layer. I'm wondering if I could place this command into sys.spawn() if I would need the call to an outside script....
Reference URL's