lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, 5 Nov 2012 16:46:36 +0800
"ms2008vip" <ms2008vip@gmail.com> wrote:

>  
> Howdy:
> 
>          I want to execute background processes concurrently from a lua script 
> like :
> 
> a = io.popen("deploy.exp" .. ip1):read("*a")
> b = io.popen("deploy.exp" .. ip2):read("*a")
> 
> where a,b are continually running processes. When i do this as above, b will only run when a is finished. And the deploy.exp script is an expect script which
>  used to ssh few servers, and execute some commands. Then I need to fetch some text from a and b. Any idea on this? 

I ran into a similar situation once: I was remote controlling long
lived processes from network sockets. My solution was the rpoll()
function in LuaPosix while using the 'lpc' Rock to run the process.
Each process was wrapped in a coroutine. It ended up looking something
like the below;  You could easily scrap the coroutine and just loop
over all your background tasks.

--
local pid, procout, procin = lpc.run("sh", "-c", full_command)

while (true) do

	local status = posix.rpoll(procin, 100)
	
	if status == 0 then
		-- wait
		coroutine.yield( )
		
	elseif status == 1 then
		-- read a line, process is done if empty
		local line = procin:read("*l")
		if not line then
			break
		end

	else
		-- failed; die
		error( "Aiiieeee! Error" )
		break
	end

end

--


-- 
Aaron B. <aaron@zadzmo.org>