[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Non-blocking read from sub-process?
- From: Sean Conner <sean@...>
- Date: Tue, 5 Apr 2016 21:43:52 -0400
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)
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.