-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding a coroutining example and explaining how it can be really useful #2848
Comments
I'm curious what I think an example of On the point of "working over telnet", though, I wonder if there's an argument to be made that |
Spot on. 👍 This exploits the feature that Lua keys can be just about any type of valuable and in this case the table itself. You do need this sort of check because
BTW build #2836 and load onto an ESP. You will find that you can just bulk paste into the UART or telnet and it just works. No data drop.
Ditto usually, but every so often if I have a "batch-like" something to do (this tree-walk is a case in point) then converting it to a CB chain can be a total PITA. What coroutining allows is another class of usecases where the procedural logic can be moved into a coroutine.
That's the advantage of having 3 priorities that you can task at. IIRC CBs run at priority 1. In general the less real time, the longer the task then the lower the priority should be, so the UART ISR posts to the input task which empties the UART into the stdin pipe at high priority. The interpreter loop runs at low priority and only processes one line at a time. This means that the stdin pipe does pick up the slack, but this also means that it is almost impossible to overflow the input. So when I was testing this |
I recently made a framework which allows to write code like for i = 1 to 10
tcp.send("Hello World")
end waiting for the gpio.write(pin, gpio.HIGH)
fw.wait(interval)
gpio.write(pin, gpio.LOW)
fw.wait(interval) where the New Methods wrapping a callback and returning its call parameters can be created easily, e.g. wrapping the |
@HHHartmann Gregor, if you leave the reference to such implementations here in an issue, they will get forgotten when the issue closes. Perhaps we should consider adding a link to the FAQ or even the cohelper README. |
This example has been merged so I am closing this issue. |
Now that I am really getting to grips with the NodeMCU Lua execution model in my bones I've realised that we can use Lua coroutining for a whole class of long running applications and still stay within the SDK strictures.
In essence the dilemma of the SDK is that tasks are recommended to be no more than 15 mSec in length and so the standard way of tackling this is by creating CB event chains, but another simple way is to use coroutining instead. For example if I do a simple recursive walk of _G, ROM and the Registry then this will crash-out with a WDT error unless I smatter the code with
tmr.wdclr()
that could in turn cause the network stack to fail. So here is how to do it using coroutining:For my minimal test build this prints out just under 400 lines with each task printing out 20; and it works fine over telnet as well.
So do you understand how this works, and should we add it as an example / FAQ discussion point?
At the end of this the Lua GC collects the lot (since the coroutine state is in an upvalue that gets descoped). You can also have other Lua and SDK CBs transparently slotting in between the slices.
Really neat!
PS: and a bonus point for anyone who can tell me what the array
s
is doing here.The text was updated successfully, but these errors were encountered: