lua-users home
lua-l archive

Re: Non-blocking read from sub-process?

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


It was thus said that the Great Dan Christian once stated:
> I want to run a sub-process that may or may-not generate output. How
> can I make sure I don't block trying to read it. I'm happy to use
> luaposix, but I'm trying not to use a big event framework like lua-ev
> (because I'm memory limited and also don't want to cross compile it).
> 
> Something like:
> 
> f = io.popen(unknown_command, "r")
> 
> while im_doing_something_else:
> data = f:read()
> if data:
> print ("Subproc says: ", data)
> 
> kill(sub_proc)
 If you're using popen() you shouldn't have to do a kill(sub_proc) unless
it's still running by the time you're finished and want to stop.
 But in anycase, the best approach is to get the underlying file
descriptor, it it non-blocking, then use posix.poll() to wait for the file
descriptor to be ready for reading, and use posix.read() for reading the
data. Under the "examples" directory in luaposix is a file "poll.lua" that
shows kind of how you do this.
 I'm not terribly familiar with luaposix (I have my own interface I use)
but that's the general gist of what you need to do.
 -spc
 
 

AltStyle によって変換されたページ (->オリジナル) /