Node-addon-api: Call function from worker thread

Created on 20 Mar 2018  Â·  8Comments  Â·  Source: nodejs/node-addon-api

Hi everyone,
I'm working on a native addon and from inside an AsyncWorker I need to call a function, in the specific I want call the function in the Execute method and I know that it's not possible. I want know if there's a simple way to do that and if my need is correlated with the following issue on n-api
https://github.com/nodejs/node/pull/17887
Thanks

Most helpful comment

All 8 comments

If you want to call into JS from another thread, then you definitely need
https://github.com/nodejs/node/pull/17887.

On Tue, Mar 20, 2018 at 12:35 PM, Nicola Del Gobbo <[email protected]

wrote:

Hi everyone,
I'm working on a native addon and from inside an AsyncWorker I need to
call a function, in the specific I want call the function in the Execute
method and I know that it's not possible. I want know if there's a simple
way to do that and if my need is correlated with the following issue on
n-api
nodejs/node#17887 https://github.com/nodejs/node/pull/17887
Thanks

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/nodejs/node-addon-api/issues/241, or mute the thread
https://github.com/notifications/unsubscribe-auth/AA7k0aLaBtcCkEKq5lrH_zx7QygBr5-jks5tgS_bgaJpZM4SyPVg
.

@gabrielschulhof Thank you for your quickly answer.

@gabrielschulhof Today I tried to call a JavaScript function from the Execute method of an AsyncWorker using uv_async_init and uv_async_send. All seems to work, but I'm asking if the runtime is capable to well track my function call. What do you think?

In the uv_async_cb on the loop thread you need a HandleScope followed by
a CallbackScope before you can call into JS.

@NickNaso, Can you share an example of calling NAPI methods in the Execute function?

Thanks. :)

@jasonrsadler the only N-API methods you can call from the Execute() function are the ones dealing with thread-safe functions. I don't believe those are covered by node-addon-api, so you would have to use them directly.

Thanks @gabrielschulhof. Could I bother you to show me a quick example?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felipezacani picture felipezacani  Â·  8Comments

rivertam picture rivertam  Â·  9Comments

Wulf picture Wulf  Â·  5Comments

bmacnaughton picture bmacnaughton  Â·  6Comments

sziraqui picture sziraqui  Â·  5Comments