3 ms·
In Lua, unlike in JavaScript, you don't need to use closures for async programming most of the time. -- SendAll -- Start sending a message on connection [c
by anonymoushn 15y ago
In Lua, unlike in JavaScript, you don't need to use closures for async programming most of the time.
-- SendAll
-- Start sending a message on connection [c]
-- Close when done.
function SendAll(c, msg)
local n=0
while n<#msg do
yield()
n=n + c.fd:send(msg, n)
end
c.fd:close()
yield('close') -- just for kicks
end
- davidhollander 15y agoCoroutines aren't as flexible as continuation passing style and add significant overhead for the non-vital syntactic sugar they give you. I'd much rather explicitly set an OnWrite event that has a one-to-one correspondence to a socket write event from a poll() system call, than create a ton of coroutines and juggle "resumes". This is especially true if you have a usecase where you want to start sending while you are still reading, this would require the creation of 2 coroutines per each context. While Lua coroutines are significantly better than Python generators by having a more functional than syntactical "yield", I still feel it does more harm than good for structuring asynchronous applications.
- silentbicycle 15y agoCoroutines are less expressive than manually doing CPS, true, but they do make many common cases simpler. They're ultimately as expressive as one-shot continuations.