lua-users home
lua-l archive

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


On Fri, May 11, 2012 at 11:10:20AM -0400, Rob Hoelz wrote:
> On Fri, May 11, 2012 at 09:56:50AM -0500, Tim Caswell wrote:
> > ...
> >
> > The one thing faux blocking never did for me is parallel work.  I like my
> > logic to be as parallel as possible to reduce latency for the http request
> > (or whatever operation I'm working on).  Using coroutines to make
> > non-blocking calls appear as sequential blocking calls is great for when
> > you want to do things in sequence. (though I would argue named callbacks
> > using JS hoisting is just as easy)  But for doing things in parallel, it's
> > rather lacking.  Maybe the best is a hybrid of using coroutines when they
> > make sense and callbacks for other stuff.
> 
> The way I would allow this is via a waitall function:
> 
>   local ev1 = ev:go(function()
>     -- do some work, like make an HTTP request or something
>   end)
> 
>   local ev2 = ev:go(function()
>     -- do some other work, like order pizza, I don't know
>   end)
> 
>   ev:waitall(ev1, ev2)
> 
> waitall would only yield control to the calling coroutine once ev1 and
> ev2 are complete.  I would probably have a waitany as well, which would
> yield control once at least one of the events is complete.

To follow up on that idea, the sleep function would be implemented as
such (or somewhat similar):

  function ev:sleep(n_seconds)
    local timer = self:timer {
        after    = n_seconds,
        callback = function(timer)
            timer:complete()
        end,
    }

    self:waitfor(timer)
  end

> 
> >
> > ...
>