Async worker and error handling documentation#272
Async worker and error handling documentation#272NickNaso wants to merge 18 commits intonodejs:masterfrom NickNaso:async-worker
Conversation
Changed version of node-addon-api on the package.json example see: [issue 206](#206 (comment))
| You are reading a draft of the next documentation and it's in continuos update so | ||
| if you don't find what you need please refer to: | ||
| [C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) No newline at end of file | ||
| Node.js native add-ons often need to execute long running tasks and to avoid of |
| [C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) No newline at end of file | ||
| Node.js native add-ons often need to execute long running tasks and to avoid of | ||
| blocking the **event loop** they have to accomplish to them in the asynchronous way. | ||
| Lets do a quick overview of how asynchronous code work in Node.js. In our model |
There was a problem hiding this comment.
I would drop the sentence Lets ... Node.js and change In our model to be `In the Node.js model'
| Node.js native add-ons often need to execute long running tasks and to avoid of | ||
| blocking the **event loop** they have to accomplish to them in the asynchronous way. | ||
| Lets do a quick overview of how asynchronous code work in Node.js. In our model | ||
| of execution we have **two threads**, the first is the **event loop** thread, that |
There was a problem hiding this comment.
'we have' -> 'there are', the node.js guidance is to avoid you, we etc.
| Lets do a quick overview of how asynchronous code work in Node.js. In our model | ||
| of execution we have **two threads**, the first is the **event loop** thread, that | ||
| represents the thread where or JavaScript code is executing in. We want avoid to | ||
| stall the event loop thread doing heavy computation so we need to create a sencond |
There was a problem hiding this comment.
'stalling the event loop thread' -> blocking other work queued on the event loop by. Therefore, we need to do this work on another thread.
| if you don't find what you need please refer to: | ||
| [C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) No newline at end of file | ||
| Node.js native add-ons often need to execute long running tasks and to avoid of | ||
| blocking the **event loop** they have to accomplish to them in the asynchronous way. |
There was a problem hiding this comment.
have to accomplish to them -> have to run them asynchronously from the event loop.
| blocking the **event loop** they have to accomplish to them in the asynchronous way. | ||
| Lets do a quick overview of how asynchronous code work in Node.js. In our model | ||
| of execution we have **two threads**, the first is the **event loop** thread, that | ||
| represents the thread where or JavaScript code is executing in. We want avoid to |
There was a problem hiding this comment.
We want avoid to -> In order to avoid
|
Hi @mhdawson When you have some time, could you review my first pass on AsyncWorker documentation? |
|
@NickNaso was out travelling this week. Will try to get to it Monday/Tuesday next week, or possibly on my travels for the summit in Berlin. |
| **event loop**. | ||
| In the Node.js model of execution there are **two threads**, the first is the | ||
| **event loop** thread, that represents the thread where JavaScript code is | ||
| executing. The node.js guidance is to avoid you blocking other work queued on the |
There was a problem hiding this comment.
Node.js should be uppercase.
"avoid you"
event loop bythread"
| asynchronously so that their methods can return in advance of the work being | ||
| completed. | ||
|
|
||
| Node Addon API provides an ABI-stable interface to support functions which covers |
There was a problem hiding this comment.
"which coversthat cover"
| completed. | ||
|
|
||
| Node Addon API provides an ABI-stable interface to support functions which covers | ||
| the most common asynchronous use cases. You have two abstract class to implement |
There was a problem hiding this comment.
The general guidance is to not formulate sentences using "you". So, we should have "You haveThere are".
"abstract classes"
|
|
||
| Node Addon API provides an ABI-stable interface to support functions which covers | ||
| the most common asynchronous use cases. You have two abstract class to implement | ||
| asynchronous operation: |
| - **[AsyncWorker](async_worker.md)** | ||
| - **[Async Context](async_context.md)** | ||
|
|
||
| These two classes help you to manage asynchronous operations through an abstraction |
| You are reading a draft of the next documentation and it's in continuos update so | ||
| if you don't find what you need please refer to: | ||
| [C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) No newline at end of file | ||
| `AsyncWorker` is an abstract class that you can subclass to remove much of the |
| if you don't find what you need please refer to: | ||
| [C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) No newline at end of file | ||
| `AsyncWorker` is an abstract class that you can subclass to remove much of the | ||
| tedious tasks on moving data between the event loop and worker threads. This |
| [C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) No newline at end of file | ||
| `AsyncWorker` is an abstract class that you can subclass to remove much of the | ||
| tedious tasks on moving data between the event loop and worker threads. This | ||
| class internally handles all the details of creating and executing asynchronous |
|
|
||
| ### Queue | ||
|
|
||
| Requests that the created work or task will be placed on the queue of the execution. |
There was a problem hiding this comment.
Perhaps instead of "placed on the queue of the execution" we could say "queued for execution".
| ObjectReference& Receiver(); | ||
| ``` | ||
|
|
||
| Returns the persistent object reference of the receiver objet set when the async |
| ``` | ||
|
|
||
| Returns the persistent object reference of the receiver objet set when the async | ||
| worker is created. |
…s about constructor
|
@gabrielschulhof @mhdawson @kfarnung I just fixed documentation as requested. if someone want take another look should be good. |
| Node.js native add-ons often need to execute long running tasks and to avoid | ||
| blocking the **event loop** they have to run them asynchronously from the | ||
| **event loop**. | ||
| In the Node.js model of execution there are **two threads**, the first is the |
There was a problem hiding this comment.
Are there actually two threads? There's the main event loop thread that's constant and worker threads as needed.
There was a problem hiding this comment.
Maybe I can rewrite the sentence like this:
In the Node.js model of execution the event loop thread represents the thread where JavaScript code is executing.
|
|
||
| ### Constructor | ||
|
|
||
| Creates new async worker. |
There was a problem hiding this comment.
nit: "Creates a new AsyncWorker"
| @@ -1,5 +1,260 @@ | |||
| # Async worker | |||
| # AsyncWorker | |||
There was a problem hiding this comment.
Can you say more about the lifetime of the AsyncWorker? I may have missed it, but I'm trying to understand exactly when/how the AsyncWorker is destroyed. I have an issue currently where Jest never exits and the uv loop has a handle count of 1. I don't know what that object is, it could be something I did in JavaScript, but the AsyncWorker I created is a suspicious candidate.
|
Believe all comments are addressed, plan to land, hopefully this afternoon. |
webern
left a comment
There was a problem hiding this comment.
Thank you for addressing my comments. Looks good and is certainly better than what we have now!
|
|
||
| ### Execute | ||
|
|
||
| This method is used to execute some tasks out of the **event loop** on a libuv |
There was a problem hiding this comment.
This should probably warn that using Napi::Objects during the Execute operation will segfault.
There was a problem hiding this comment.
I think I covered that with As the method is not
+running on the main event loop, it must avoid calling any methods from node-addon-api
+or running any code that might invoke JavaScript.
PR-URL: #272 Reviewed-By: Kyle Farnung <kfarnung@microsoft.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
|
Landed as 5a63f45 |
PR-URL: nodejs/node-addon-api#272 Reviewed-By: Kyle Farnung <kfarnung@microsoft.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
PR-URL: nodejs/node-addon-api#272 Reviewed-By: Kyle Farnung <kfarnung@microsoft.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
PR-URL: nodejs/node-addon-api#272 Reviewed-By: Kyle Farnung <kfarnung@microsoft.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
PR-URL: nodejs/node-addon-api#272 Reviewed-By: Kyle Farnung <kfarnung@microsoft.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This is a first step on writing AsyncWorker documentation. Please review the document and suggest some improvements.