lua-users home
lua-l archive

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


Hi guys,

Here's a little hack that may be useful; I've added a simplified
binding to poll() to lposix. It takes an input file, and a timeout
(which may be -1 for 'infinite' as usual). It will return either 0
(timeout), 1 (file ready for reading) or nil, we had a problem.

It's useful for a situation where you have a little daemon doing
something occaisionally, which you also want to control using a named
pipe. Obviously this can be done with other methods, and obviously
there are other interesting uses, but that's what I did it for. A
particularly cool combination is posix.rpoll() and posix.fork() for
old-style multi-threading.

require 'posix'

f = io.open('one')
while true do
    ret = posix.rpoll(f,500)
    if ret == 0 then
        print 'timeout'
    elseif ret == 1 then
        line = f:read()
        print(line)
    else
        print 'finis!'
        return
    end
end

This contains the updated lposix.c and testpoll.lua; look for the
function Ppoll().

Note that this is an out-of-the-box Lua 5.1 compliant lposix.c, so
compile it to 'posix.so' and require() will find it _without_
posix.lua

cc -shared -o posix.so -I<lua-include> lposix.c

http://mysite.mweb.co.za/residents/sdonovan/lua/posix.zip

steve d.