wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

Clarification about using in http server #53

Closed dtryon closed 4 years ago

dtryon commented 4 years ago

Hi, and thank you for that awesome work. The api is perfect for my use case (off loading some CPU intensive calcs).

Is there some guidance on how to setup a web server to use microjob?

for example, say I'm using koa. Should I use start() next to the import at the top of a module? This will cause the worker pool to be global. Or should I call await start() and await stop() on every request?

Thank you.

wilk commented 4 years ago

Hi @dtryon! Thanks for using microjob: hope it is helping you with your job 💪 Well, I'd recommend starting the thread pool before calling starting the Koa server (app.listen to be clear). With the thread pool working for all the incoming requests, you can improve performance in terms of context switching between the main thread and the worker.

wilk commented 4 years ago

Well, no. The worker thread starts at the beginning, before the Koa server. This way it will be available for each request: when you need to off load some CPU computation, just send the request to the worker thread through microjob.