lua-users home
lua-l archive

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


 On 07/09/10 12:41, pj@pjb.com.au wrote:
OK, one more on the subject:

Problem is, popen
doesn't give you the return value of the command you ran.
The good news is that closing the handle from io.popen in
Lua 5.2 does give you the return code.
That it means p:close waits for the command to finish:

   local p = assert(io.popen('aplaymidi -', 'w'))
   p:write(my_midi)  -- fast; the MIDI is compact :-)
   p:close()         -- waits several minutes :-(

Is there a way of letting aplaymidi run on in the background?
   p = io.popen('aplaymidi -&', 'w')
   p:write(my_midi)
fails with
   - is not a Standard MIDI File   (it's empty :-( )
and I get the same message from:
   io.popen('sh -c "aplaymidi -"&', 'w')
   p:write(my_midi)
and likewise at the command-line:
   cat t.mid | sh -c 'aplaymidi -&'

Although at the command-line:
   cat t.mid | sh -c "aplaymidi -"&
works as intended, so by now I'm starting to get confused...

Is there a right way to background the aplaymidi process ?

Regards,  Peter Billam

http://www.pjb.com.au       pj@pjb.com.au      (03) 6278 9410
"Was der Meister nicht kann,   vermöcht es der Knabe, hätt er
  ihm immer gehorcht?"   Siegfried to Mime, from Act 1 Scene
Due to the shortcomings of Lua's support for creating child processes, I started a Lua module for doing this, modelled loosely on Python's subprocess module. It is able to create child processes with pipe/file redirection, and it can wait/poll for child processes. It can use POSIX (fork/exec) or the Windows API.

The 'subprocess.wait' function, which waits for any child process to finish, doesn't work very well in a multi-threaded POSIX environment, because I can't give the POSIX 'wait' function a list of pids to wait for: if 'wait' returns a pid for which there's no Lua object ('alien pid'), the process has already been removed from the process table and there's little I can do.

I'm working on a locked global data structure to store alien pids, but that won't help if other parts of the process are using wait or waitpid.

Apart from those shortcomings with subprocess.wait (which don't apply to Windows because there is WaitForMultipleObjects), it works quite well.

Code and documentation are in this repository: http://github.com/xlq/lua-subprocess