Skip to content

Async worker and error handling documentation#272

Closed
NickNaso wants to merge 18 commits intonodejs:masterfrom
NickNaso:async-worker
Closed

Async worker and error handling documentation#272
NickNaso wants to merge 18 commits intonodejs:masterfrom
NickNaso:async-worker

Conversation

@NickNaso
Copy link
Copy Markdown
Member

This is a first step on writing AsyncWorker documentation. Please review the document and suggest some improvements.

@NickNaso NickNaso changed the title Async worker Async worker documentation May 17, 2018
Comment thread doc/async_operations.md Outdated
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove of

Comment thread doc/async_operations.md Outdated
[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
Copy link
Copy Markdown
Member

@mhdawson mhdawson May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop the sentence Lets ... Node.js and change In our model to be `In the Node.js model'

Comment thread doc/async_operations.md Outdated
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
Copy link
Copy Markdown
Member

@mhdawson mhdawson May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'we have' -> 'there are', the node.js guidance is to avoid you, we etc.

Comment thread doc/async_operations.md Outdated
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'stalling the event loop thread' -> blocking other work queued on the event loop by. Therefore, we need to do this work on another thread.

Comment thread doc/async_operations.md Outdated
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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have to accomplish to them -> have to run them asynchronously from the event loop.

Comment thread doc/async_operations.md Outdated
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove 'or' and 'in'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want avoid to -> In order to avoid

@NickNaso
Copy link
Copy Markdown
Member Author

Hi @mhdawson do I consider to add resource_name and resource like reported here #253 on on the documentation for AyncWorker?

@NickNaso
Copy link
Copy Markdown
Member Author

Hi @mhdawson When you have some time, could you review my first pass on AsyncWorker documentation?

@mhdawson
Copy link
Copy Markdown
Member

@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.

Comment thread doc/async_operations.md Outdated
**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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js should be uppercase.

"avoid you"

event loop bythread"

Comment thread doc/async_operations.md Outdated
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"which coversthat cover"

Comment thread doc/async_operations.md Outdated
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
Copy link
Copy Markdown
Contributor

@gabrielschulhof gabrielschulhof Jun 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general guidance is to not formulate sentences using "you". So, we should have "You haveThere are".

"abstract classes"

Comment thread doc/async_operations.md Outdated

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"operations"

Comment thread doc/async_operations.md Outdated
- **[AsyncWorker](async_worker.md)**
- **[Async Context](async_context.md)**

These two classes help you to manage asynchronous operations through an abstraction
Copy link
Copy Markdown
Contributor

@gabrielschulhof gabrielschulhof Jun 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"help you to"

Comment thread doc/async_worker.md Outdated
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"muchmany"

Comment thread doc/async_worker.md Outdated
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"onof"

Comment thread doc/async_worker.md Outdated
[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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"an asynchronous"

Comment thread doc/async_worker.md Outdated

### Queue

Requests that the created work or task will be placed on the queue of the execution.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps instead of "placed on the queue of the execution" we could say "queued for execution".

Comment thread doc/async_worker.md Outdated
ObjectReference& Receiver();
```

Returns the persistent object reference of the receiver objet set when the async
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"object"

Comment thread doc/async_worker.md Outdated
```

Returns the persistent object reference of the receiver objet set when the async
worker is created.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"iswas"

Copy link
Copy Markdown
Contributor

@gabrielschulhof gabrielschulhof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!

@NickNaso NickNaso changed the title Async worker documentation Async worker and error handling documentation Jun 12, 2018
@NickNaso
Copy link
Copy Markdown
Member Author

@gabrielschulhof @mhdawson @kfarnung I just fixed documentation as requested. if someone want take another look should be good.

Comment thread doc/async_operations.md Outdated
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there actually two threads? There's the main event loop thread that's constant and worker threads as needed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's better.

Comment thread doc/async_worker.md Outdated

### Constructor

Creates new async worker.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "Creates a new AsyncWorker"

Comment thread doc/async_worker.md
@@ -1,5 +1,260 @@
# Async worker
# AsyncWorker
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mhdawson
Copy link
Copy Markdown
Member

Believe all comments are addressed, plan to land, hopefully this afternoon.

Copy link
Copy Markdown

@webern webern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for addressing my comments. Looks good and is certainly better than what we have now!

Comment thread doc/async_worker.md

### Execute

This method is used to execute some tasks out of the **event loop** on a libuv
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably warn that using Napi::Objects during the Execute operation will segfault.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

mhdawson pushed a commit that referenced this pull request Jun 14, 2018
PR-URL: #272
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
@mhdawson
Copy link
Copy Markdown
Member

Landed as 5a63f45

@mhdawson mhdawson closed this Jun 14, 2018
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
PR-URL: nodejs/node-addon-api#272
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
PR-URL: nodejs/node-addon-api#272
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
PR-URL: nodejs/node-addon-api#272
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
PR-URL: nodejs/node-addon-api#272
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants