-
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
Example Lua module for coroutining #2851
Conversation
@galjonsfigur, perhaps you might to review this as well 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for review invite!
The module itself looks good! I used coroutines in some of my Lua an LuaJIT projects and they seem to be even better fitted for NodeMCU constraints. I'm glad that there will be a documentation and example how to use them. Great work!
functionality as described in the [Lua RM §2.11](https://www.lua.org/manual/5.1/manual.html#2.11) and [PiL Chapter 9](https://www.lua.org/pil/9.html). | ||
|
||
The NodeMCU Lua VM fully supports the standard coroutine functionality. Any | ||
interactive or callback tasks are executed in the default thread, and the coroutine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is no good calling this a thread. As the PiL also points out it is only similar. A thread is executed concurrently. So taffeta will get confused. Maybe use "execution context" or whatever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A thread is executed concurrently.
Gregor, I must disagree on this one. Until we had multi-processor CPUs. threads would never be executed concurrently, since there was only one processing unit. The WP thread article has a nice definition "a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler". Features such as concurrence and pre-emption are more emergent benefits of this independence. In our Lua world the machine is the Lua VM and this must, like node.js, run in a single OS thread.
What I am trying to do is to explain how this works to the average IoT developer. If you can suggest better wording then I'll adopt this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The term to use, I think, is "cooperative multi-threading", assuming I am correct that, on our C substrate, a Lua task will run to completion, without preemption and without concurrent access to the Lua VM and its heap, even if tasks are being dispatched by multiple threads. I'm not sure that this module's documentation is the right place to spell it out; perhaps node.task.post()
is the right place and this should simply cite that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used stack instead of thread.
docs/lua-modules/cohelper.md
Outdated
statements, and since the order of SDK tasks is indeterminate, the application | ||
must take care to handle any ordering issues. This particular example uses | ||
the `node.task.post()` API with the `taskYield()`function to resume itself, | ||
so the running code can simple call `taskYield()` at regular points in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about my English but should it be"simply"
docs/lua-modules/cohelper.md
Outdated
|
||
### Release | ||
|
||
Not required. All resoruces are release on completion of the `exec()` method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
released
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And resources
docs/lua-modules/cohelper.md
Outdated
Not required. All resoruces are release on completion of the `exec()` method | ||
|
||
## `cohelper.exec()` | ||
Execute a function which is wrapper by a coroutine handler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrapped
docs/lua-modules/cohelper.md
Outdated
`require("cohelper").exec(func, <params>)` | ||
|
||
#### Parameters | ||
- `func`: Port number for HTTP server. Most HTTP servers listen at port 80. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, cut and past crud. Needs removing.
I will leave it for another day or for any other feedback before pushing the updates. |
I just aded it to the milestone... |
Fixes #2848
dev
branch rather than formaster
.docs/*
.See #2848 and supplied docs for explanation.