Parallelizing tasks in Node.js

后端 未结 5 1518
执念已碎
执念已碎 2020-12-13 02:48

I have some tasks I want to do in JS that are resource intensive. For this question, lets assume they are some heavy calculations, rather then system access. Now I want to r

5条回答
  •  无人及你
    2020-12-13 03:26

    Keep in mind I/O is parallelized by Node.js; only your JavaScript callbacks are single threaded.

    Assuming you are writing a server, an alternative to adding the complexity of spawning processes or forking is to simply build stateless node servers and run an instance per core, or better yet run many instances each in their own virtualized micro server. Coordinate incoming requests using a reverse proxy or load balancer.

    You could also offload computation to another server, maybe MongoDB (using MapReduce) or Hadoop.

    To be truly hardcore, you could write a Node plugin in C++ and have fine-grained control of parallelizing the computation code. The speed up from C++ might negate the need of parallelization anyway.

    You can always write code to perform computationally intensive tasks in another language best suited for numeric computation, and e.g. expose them through a REST API.

    Finally, you could perhaps run the code on the GPU using node-cuda or something similar depending on the type of computation (not all can be optimized for GPU).

    Yes, you can fork and spawn other processes, but it seems to me one of the major advantages of node is to not much have to worry about parallelization and threading, and therefor bypass a great amount of complexity altogether.

提交回复
热议问题