-
Notifications
You must be signed in to change notification settings - Fork 281
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
N-API Threadsafe call asynchronous javascript callback #1412
Comments
@gabrielschulhof do you have an existing example you can point @wilhelmmatilainen to? Might make sense to add one to the https://github.com/nodejs/abi-stable-node-addon-examples |
I have already accomplished this by getting the returned napi_promise_then(env, promise, my_success_callback, my_fail_callback); |
@wilhelmmatilainen you can There's no straight-forward way of doing that, because, by definition, the thread-safe function call is asynchronous. So, to convey the result of the call into JavaScript back, your best bet is to create a Of course, if you have multiple secondary threads, you need to know which value on the |
That is exactly how I am doing it. Except the queue has size 1 and it is managed by node.js. Depending on what is needed, the native side blocks or keeps returning EGAIN or EWOULDBLOCK until the result is ready. |
@wilhelmmatilainen can you link to the code in question (if it's open source)? |
The project is not at least yet public and there is still problems to be solved until it could be used. However, I could probably strip unnecessary code and leave the meaningful parts if needed. It is still not hard to accomplish. |
@wilhelmmatilainen please, if you can, that would help a great deal 🙂 |
Here it is: https://github.com/wilhelmmatilainen/node-addon-api-async-tsf |
@wilhelmmatilainen it looks like you need round-trip asynchronous calling, not just from the secondary thread to the JS thread. |
That is correct, as I originally asked. |
@wilhelmmatilainen if you wish to establish the return value after multiple trips into JS, you need to use
Please let me know if this answers your question! |
@wilhelmmatilainen is it possible that you share some details of the project in which you are using While it's still experimental, we can also tweak the API. Are there any |
I am using threadsafe functions for wrapping whole separate programs into
node.js modules. This way I have more direct control to the program than an
api or some communication layer provides if there even is such thing. The
programs may be asynchronous or not and mostly some parts are while there
is also synchronous calls. So, the calls from the program to javascript
should always be possibly asynchronous from node.js perspective and when
returning the value the program should behave correctly regardless whether
it expected the call to be asynchronous or synchronous. Basically this
means just blocking for synchronous operation and for asynchronous
operation it means returning EAGAIN or EWOULDBLOCK until the javascript
call has returned completely with the promises resolved or rejected.
Maybe if there were a simple function to call javascript from separate thread "directly" with one function and the final result would be retrieved asynchronously when ready. If a promise was returned then the promise would automatically be resolved or rejected before implying the call being ready.
```c
call_js(env, my_function, argc, argv, &result)
while(result->ready == 0)
do_something_else_or_just_wait_idle()
// result is now ready and if a promise is returned, the promise is resolved or rejected. Meaning that result has the final value or error, never another promise.
// OR
// save non-resolved result promise for later resolution and return EGAIN or EWOULDBLOCK
```
|
@wilhelmmatilainen I have created an example showing an asynchronous round trip: Do you think we should incorporate any of the functionality shown in the example into a new N-API? |
@wilhelmmatilainen the round trip example has now landed on master in nodejs/node-addon-examples: Does it serve your needs? |
Yes and thank you for making the example. |
Excellent 🙂 |
Version: v10.8.0
Platform: Linux 4.17.11 x86_64 GNU/Linux
By using the new threadsafe functions I am able to call javascript from another thread and return synchronously a value (e.g. a number) from javascript to another thread.
What is the proper way to call a threadsafe function -> pass arguments to javascript -> await for a result -> return the result -> pass the result back to another thread?
The text was updated successfully, but these errors were encountered: