lua-users home
lua-l archive

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

I don't know how horrible a solution you might think this is, but under Linux and Darwin, I've resorted to adding a "peek to see how many characters are pending" function for file descriptors using the FIONREAD ioctl (I've only used it for serial ports and stream sockets, I think I've used it with popen as well).

Beware, as this doesn't work for all devices and should not be misused to poll a device.

My typical use-case is to select() on a file descriptor, then use FIONREAD to get the pending character count, then read that number of bytes and repeat.

This can all be made luasocket friendly (although from memory, I don't remember how I got a file descriptor from the Lua file userdata... I may have cheated with my own access function).

If you're interested in this approach I can share example code.


On Tue, Dec 6, 2011 at 4:20 PM, Tim Caswell <> wrote:
Not sure if it's useful, but my Luvit project has a fully non-blocking
popen with full I/O streams for stdin, stdout, and stderr as well as
events for process exit and/or signal termination.  Just don't try to
use luvit as a library to your existing lua program.  luvit is it's
own runtime (that embeds luajit) and makes many assumptions.


On Tue, Dec 6, 2011 at 4:17 PM, Peter Odding <> wrote:
>> We are running an external process using io.popen(). Something like ...
>>  ecom = assert(io.popen(cmd, 'r'))
>> Is there a way to setup non-blocking reads with this type of interface?
>> Or do other lua modules offer more control? What we've seen is that our
>> read() command will block if the external process does not have any data
>> to return. We need a strategy to make this non-blocking.
>> Thanks for any suggestions,
>> Joe
> I don't know whether it's possible or easy to make pipes created by
> io.popen() non-blocking, however my Lua/APR binding [1] supports process
> management and pipes with configurable blocking behaviour (non-blocking,
> blocking and blocking with time out). It works in Windows and UNIX
> environments (and then some).
>  - Peter
> [1]