最近在不断的理解nodejs。
经常看到nodejs不适用于CPU密集型计算的场景,那我们在nodejs中也经常使用异步编程async来执行耗时的查询操作,查询完,再返回就可以了。是不是异步编程来解决CPU密集型计算的问题?
结论:
异步编程是用来解决IO的。
js是单线程,cpu密集型计算还是会耗时。
巴扎黑2017-04-17 12:08:20
No, you must understand that javascript has only one execution thread. As long as it enters the calculation-intensive area, the CPU will be occupied and the entire program will be stuck here. The reason why io can use asynchronous acceleration is that the CPU is not occupied during the delay period of io.
黄舟2017-04-17 12:08:20
No, asynchronous programming is specifically designed to solve IO-intensive problems. When io is blocked, use asynchronous events to release CPU resources for other tasks.
In essence, asynchronous programming tends to use CPU resources as much as possible, so it is completely meaningless for CPU-intensive programs. . .
Parallel computing and distributed computing should be considered at this time.