2

I'm developing a node.js app which is fully non-blocking.

There are however a few CPU intensive functions:

  • CryptoJS.AES decrypt/encrypt
  • uuid creation
  • create content from data

Now I found a module to use threads to offload the event loop from the CPU intensive tasks: node-webworker-threads

Should I now create:

  1. At app boot: one thread per function, so AES.decrypt is a thread, as is AES.encrypt, etc.
  2. At app boot: a thread pool per function? (how many threads? 1 per CPU core?)
  3. At execution: a new thread on entering of each function, and destroy it after completion?

Threads are something I don't yet fully understand..

4

1 回答 1

5

It really depends on your application. If you use a fixed-size thread pool, you will have to implement a queue if the number of requests > number of available threads, which may cause some congestion.

The best way (in my opinion of course) would be to spawn a new thread for each request, that deals with the AES, UUID and data. That is, one thread per request that performs all calculations. When everything is done, return the results from the thread and kill it.

This may of course spawn many threads on a high-load server; but will keep your event-loop clear.

EDIT: This thread might be interesting for you.

于 2013-10-13T22:28:40.060 回答