yocontra / node-gdal-next

Node.js bindings for GDAL (Geospatial Data Abstraction Library) [Fork]
https://contra.io/node-gdal-next/
Apache License 2.0
75 stars 36 forks source link

Node.js Worker Thread compatibility #3

Open thom4parisot opened 4 years ago

thom4parisot commented 4 years ago

When I use Node.js Worker Thread to offload the work in another process, I encounter this error :

Error: Module did not self-register: '/path/to/app/node_modules/gdal-next/lib/binding/node-v72-darwin-x64/gdal.node'.
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1207:18)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Module.require (internal/modules/cjs/loader.js:1043:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/Users/oncletom/workspace/cartobio/front/node_modules/gdal-next/lib/gdal.js:26:31)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)

The document specifies this:

Native add-ons can only be loaded from multiple threads if they fulfill certain conditions:

(…)

  • Be an N-API addon, or
  • Be declared as context-aware using NODE_MODULE_INIT() as described above.

There is a code example that makes a native module "context aware".

yocontra commented 4 years ago

@oncletom That's a great item for the roadmap - I do have on there to switch to N-API, but I will look into declaring it as a context-aware native module as an interim solution. I would also love to have this for our production cases.

thom4parisot commented 4 years ago

Cool! Let me know if you need some testing — I kept my code while switching to worker-farm in the meantime.

I found Worker Thread integration easier (to write and debug) and less verbose to achieve identical result.

yocontra commented 4 years ago

If you have the time and want to try it out feel free to send a PR - I'm pretty busy this week and probably won't get around to this until next week.