lua-users home
lua-l archive

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


I'm doing a similar thing wrapping libuv (which is what Luvit is built on) in a Lua module. All the potentially blocking calls, such as filesystem IO, become coroutine yield/resume when invoked from within a coroutine, making the code look very clean indeed. 

Libuv is pretty nice I have to say, and offers everything you're looking for AFAICT (including working on Windows). It also doesn't have to take over the main thread loop, so can be integrated with mainloop-hungry GUI APIs. 

I guess I should put this code somewhere for people to look at - but would need to clean it up first... 

On Mar 28, 2012, at 4:56 AM, steve donovan wrote:

> On Wed, Mar 28, 2012 at 1:37 PM, Axel Kittenberger <axkibe@gmail.com> wrote:
>> The more you work with immutables, the easier it gets.
> 
> True, but using Lua we are restricted, and luvit is a similar kind of
> solution to the single-threaded issue as Node.js is for JavaScript.
> 
> I must say that _coroutines_ make writing async-style code so much
> more pleasant. For instance, in this snippet that sets up a Windows
> named pipe server:
> 
> winapi.make_pipe_server_async(function(f)
>    while true do
>        local res = f:read()
>        if res == 'close' then break end
>        f:write(res:upper())
>    end
>    print 'finis'
> end)
> 
> the file object f is wrapped (a la Copas) so that it is not actually
> blocking on reads. Getting the write to be non-blocking continues to
> be an interesting exercise...
> 
> Similar tricks are possible for making waiting-on-processes coroutine-friendly.
> 
> steve d.
>