Skip to content
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

Async worker and error handling documentation #272

Closed
wants to merge 18 commits into from
Closed

Async worker and error handling documentation #272

wants to merge 18 commits into from

Conversation

NickNaso
Copy link
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
Node.js native add-ons often need to execute long running tasks and to avoid of
Copy link
Member

Choose a reason for hiding this comment

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

remove of

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
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'

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

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

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

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
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
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
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
Member Author

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

@mhdawson
Copy link
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.

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
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"

completed.

Node Addon API provides an ABI-stable interface to support functions which covers
Copy link
Contributor

Choose a reason for hiding this comment

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

"which coversthat cover"


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
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"

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
Contributor

Choose a reason for hiding this comment

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

"operations"

- **[Async Context](async_context.md)**

These two classes help you to manage asynchronous operations through an abstraction
Copy link
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"

`AsyncWorker` is an abstract class that you can subclass to remove much of the
Copy link
Contributor

Choose a reason for hiding this comment

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

"muchmany"

`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
Contributor

Choose a reason for hiding this comment

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

"onof"

`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
Contributor

Choose a reason for hiding this comment

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

"an asynchronous"

### Queue

Requests that the created work or task will be placed on the queue of the execution.
Copy link
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".

```

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

Choose a reason for hiding this comment

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

"object"


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

Choose a reason for hiding this comment

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

"iswas"

Copy link
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
Member Author

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

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

### Constructor

Creates new async worker.
Copy link
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"

@@ -1,5 +1,260 @@
# Async worker
# AsyncWorker
Copy link

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
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
Member

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

Copy link

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

### Execute

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

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