4 ms·
Once you have this callback-based approach at a relatively stable level, it would be interesting to then take advantage of Lua's coroutines. IMHO Lua has one of
by zacharyvoase 14y ago
Once you have this callback-based approach at a relatively stable level, it would be interesting to then take advantage of Lua's coroutines. IMHO Lua has one of the best implementations of coroutines in recent languages, and adding libuv to that mixture could be dangerous (in a good way).
- philips 14y agoTim Caswell, who started luvit, has been exploring ideas along those lines: http://lua-users.org/lists/lua-l/2012-10/msg00181.html http://lua-users.org/lists/lua-l/2012-10/msg00181.html
- dyselon 14y agoA while back I started to mess around with lua + libuv, and found it pretty easy to write "blocking" functions that would yield to its caller a function responsible for setting up the requisite events to wake itself back up. If you want to see what this would look like, it's extremely ugly since I didn't work on it much (as it was easier to just use luvit and node-fibers), but I did put it up on github at http://github.com/dyselon/loquat http://github.com/dyselon/loquat, and you can see examples of it in the timer example of the readme, and the uncommented parts of examples/test.lua. Maybe it'll be of some use or inspiration to someone. There a lot of cruft in those examples, but in practical usage, everything but your blocking calls would be hidden behind libraries. It's pretty easy to set up similar fiber-friendly wrappers for your favorite node.js libraries, too, if you're fond of the node-fibers add-on.
- leafo 14y agoI also agree this is a very powerful approach. If you'd like to see it in action already you should check out [OpenResty](http://openresty.org/ http://openresty.org/). It's a platform for doing Lua directly on top of nginx. Each request can run Lua from within a coroutine, and and you can issue nginx subrequests which block Lua code but don't block the sever. See [ngx.location.capture](http://wiki.nginx.org/HttpLuaModule#ngx.location.capture http://wiki.nginx.org/HttpLuaModule#ngx.location.capture) and [ngx.location.capture_multi](http://wiki.nginx.org/HttpLuaModule#ngx.location.capture_multi http://wiki.nginx.org/HttpLuaModule#ngx.location.capture_mul...) for issuing multiple in parallel. nginx's subrequests can do just about anything with the right modules, from database queries to HTTP requests. Effectively turning nginx into your event loop. It's a very interesting idea. I've just started using it, but I'm excited to make something with it.