Example wait.lua

require 'winapi'

local k,processes,outputs = 0,{},{}
local append, remove = table.insert, table.remove

function queue_drain()
    local idx,err = winapi.wait_for_processes(processes,false)
    if err then return nil, err end
    local item, p = outputs[idx], processes[idx]
    item.callback(item.read:read(),p:get_exit_code())
    item.read:close()
    p:close()
    remove(processes,idx)
    remove(outputs,idx)    
end

function queue_process(cmd,nc,finish,callback)  
  -- if there's already nc commands in the process queue,
  -- then we just block, otherwise add a new command
  if #processes == nc then
	queue_drain()
  end
  if #processes < nc then
	  local p,r = winapi.spawn_process(cmd)
	  append(outputs,{read=r,callback=callback})
	  append(processes,p)
  end
  if finish then 
    repeat queue_drain() until #processes == 0
  end
end

for i = 1,7 do
  queue_process('lua slow.lua '..i,2,i==7,print)
end

generated by LDoc 1.2