Is event-based code possible? #87
-
If I wanted to write some code that gets executed asynchronously, like using Can coroutines be used in such way that could emulate such behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
If you want to write evented code, you need an event loop. I think a way to do that in Nelua that would be idiomatic would involve a coroutine scheduler. If you're familiar with the Lua ecosystem, I think cqueues would be a good example of this. |
Beta Was this translation helpful? Give feedback.
-
I see three approaches to write asynchronous code:
Callback hell you can mostly do in any language, just use callbacks for your asynchronous code, this can work fine and probably is the most efficient way, the problem with it is that the code can become unreadable, and tracking state across state continuations can become a complex, leading to spaghetti code and bugs, it's not ideal to write asynchronous code using just callbacks, hence the name. Stackless coroutines allows you to do continuations of functions, but you have limitations such as needing to explicit mark a function as async and then await can only be used in its scope following some rules, in many implementations this way will usually break when using some natural code, like when trying to await in a subroutine. While it has some limitations it's very lightweight approach as it doesn't have to allocate a true stack frame, switching coroutine context is trivial, and can be supported in any platform. In summary, stackless coroutines uses less memory, less instructions to do context switches, and is more portable, but usually you cannot write natural code, because under the hood async functions are not true functions, they are kind of "fake" functions. Stackful coroutines like Lua and Nelua have via the Nelua chooses stackful coroutines due to its Lua influence, and in my biased opinion the price pays off, as it can be simpler to use and powerful.
Like @ryanford said, to put coroutines to work you need some event loop first that generates callbacks so you can resume coroutines based on events. A popular, cross platform, and widely available even loop library is libuv, for instance it is used by node.js in Javascript world and Luvit in the Lua ecosystem, there are bindings for it in nelua-decl, plus there is an example of using it with Nelua coroutines in this example. A lightweight, popular among embedded devices, cross platform, easy to use event loop library for networking only is mongoose, it's used a lot in the IoT world, it's just a single C file thus very easy to setup. Again there are bindings for it in There are many other event loop library choices for C you could use, just generate the bindings. Or in the game development context, it's common to make your own event loop and scheduler, where you could use coroutines to do game logic based of events. |
Beta Was this translation helpful? Give feedback.
-
@ryanford and @edubart, thank you both for your informative replies. |
Beta Was this translation helpful? Give feedback.
I see three approaches to write asynchronous code:
async
/await
in some languages like Javascript/Nim)Callback hell you can mostly do in any language, just use callbacks for your asynchronous code, this can work fine and probably is the most efficient way, the problem with it is that the code can become unreadable, and tracking state across state continuations can become…