[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Has Lua reached perfection?
- From: Sean Conner <sean@...>
- Date: Mon, 29 Jan 2018 02:11:01 -0500
It was thus said that the Great Paige DePol once stated:
>
> I still need to learn more about co-routines, I get what they are but I
> don't think the entire concept of when and how to use them has clicked
> quite yet.
A good example would be event programming for a network based server.
Each "connection" or "request" can be handled by a co-routine. The examples
I have are rather large, but generally, they look like (very pseudocodeish
here, even for Lua):
function handler(remote,data)
do_something_that_doesnt_block(data)
remote,data = coroutine.yield(remote,data)
do_something_else(remote,data)
runnable[remote] = nil
end
function mainloop(socket)
data,raddr = socket:read()
if newrequest(data) then
co = coroutine.create(handler)
runnable[raddr] = co
else
coroutine.resume(runnable[raddr],raddr,data)
end
return mainloop(socket)
end
At least, that's how the code I've written has been generally structured
(although more verbose).
> That said, I would love to see if it would be possible to
> implement a version of libdispatch (Grand Central Dispatch in macOS)
> to Lua along with creating "blocks" of code... which are really just
> closures with slightly different upvalue scope rules if I recall.
I don't see why not.
-spc (And I don't even know what Grand Central Dispatch is)