I am confused with node.js' advantages over other tech. I've been reading this article : http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js and this How to decide when to use Node.js? to familiarize myself with it and have left me confused.
我和node搞混了。js相对于其他技术的优势。使我熟悉它,使我感到困惑。
I am familiar with cpu intensive task like the computation of the Fibonacci series but that's where my understanding ends.
我熟悉cpu密集型的任务,比如斐波那契数列的计算,但我的理解到这里就结束了。
For example, I have a Rest API that does all the computation or recommendation and is housed on a different server from the machine running node, then node.js won't have any trouble with having to deal with cpu intensive task. Just call the api then tell the client that your request is acknowledged.
例如,我有一个Rest API,它执行所有的计算或推荐,并且驻留在与运行节点(然后是节点)不同的服务器上。js在处理cpu密集型任务时不会有任何问题。只需调用api,然后告诉客户端您的请求已被确认。
I can't shake this thinking about comparing node.js with a simple ajax call to send the request from a form to the server, display a ticker then show the result. I am guessing that node.js is a web server, doing lot's of "ajax" type calls and handling concurrent connections.
我无法摆脱这种比较节点的想法。js通过一个简单的ajax调用将请求从表单发送到服务器,然后显示一个ticker,然后显示结果。我猜是那个节点。js是一个web服务器,执行大量的“ajax”类型调用并处理并发连接。
Are my assumptions correct?
我的假设是正确的吗?
Is it also correct to assume that retrieving data from a database is an io operation but creating a complex report from that data a cpu intensive one?
假设从数据库中检索数据是io操作,但从数据中创建一个复杂的报告是cpu密集型的报告,这种假设是否正确?
3 个解决方案
#1
2
You are right about handling many ajax requests, however thats true in worker based model also (php/python workers threads)
在处理许多ajax请求时,您是正确的,但是在基于工作者的模型中也是如此(php/python工作者线程)
Main difference for event based system there will be only one worker doing all sorts of computation part of code (such as filtering data, adding processing etc). When it calls io ops like read from file, or db etc. node doesn't have control over that, instead of waiting on that to finish it puts a call back in the queue and moves on with next processing in queue (if any).
基于事件的系统的主要区别在于,只有一个工作人员在执行代码的所有计算部分(如过滤数据、添加处理等)。当它调用io操作时,比如从文件中读取,或者db等节点不能控制它,而不是等待它完成,它会在队列中返回一个调用,并继续进行队列中的下一个处理(如果有的话)。
For analogy think of pizza outlet, if only one person is taking order and handing over the order to kitchen, once its ready cutting it, packing and giving it to customer. Where ever there is wait, he just moves on to next task. This is what node does, that person wont hang-on next to kitchen until pizza gets cooked.
比方说披萨店,如果只有一个人在做订单,把订单交给厨房,一旦它准备好了,就把它包装好,交给顾客。无论何时有等待,他都会继续下一个任务。这就是node要做的,那个人在披萨被煮熟之前不会挂在厨房旁边。
In case of worker based approach think of a bank teller and you see couple of them (may be 5 or so) they take every kind of request but they dont switch between customer / request.
如果以工作为基础的方法,想象一个银行出纳员,你看到他们中的几个(可能是5个左右)他们接受各种各样的请求,但是他们不会在客户/请求之间切换。
#2
1
Refer to these resources for a deeper understanding of how JavaScript event loop works.
请参阅这些参考资料,以进一步了解JavaScript事件循环是如何工作的。
https://www.youtube.com/watch?v=8aGhZQkoFbQ
https://www.youtube.com/watch?v=8aGhZQkoFbQ
http://latentflip.com/loupe/
#3
0
I can't answer all your doubts, but would like you to have some clarity over AJAX. AJAX - Asynchronous JavaScript + XML is a technique to make requests to a server. Nodejs server knows how to handle such requests, but saying that is the only thing it can do is absolutely wrong. Nodejs is single threaded, hence async. Whether it is good for CPU intensive tasks, I would say why not, unless you want to solve issues in a multithreaded fashion.
我无法回答你所有的疑问,但希望你能对AJAX有所了解。AJAX -异步JavaScript + XML是向服务器发出请求的一种技术。Nodejs服务器知道如何处理此类请求,但说这是它惟一能做的事情是绝对错误的。Nodejs是单线程的,因此是异步的。不管它是否适合CPU密集型任务,我想说为什么不,除非您希望以多线程的方式解决问题。
#1
2
You are right about handling many ajax requests, however thats true in worker based model also (php/python workers threads)
在处理许多ajax请求时,您是正确的,但是在基于工作者的模型中也是如此(php/python工作者线程)
Main difference for event based system there will be only one worker doing all sorts of computation part of code (such as filtering data, adding processing etc). When it calls io ops like read from file, or db etc. node doesn't have control over that, instead of waiting on that to finish it puts a call back in the queue and moves on with next processing in queue (if any).
基于事件的系统的主要区别在于,只有一个工作人员在执行代码的所有计算部分(如过滤数据、添加处理等)。当它调用io操作时,比如从文件中读取,或者db等节点不能控制它,而不是等待它完成,它会在队列中返回一个调用,并继续进行队列中的下一个处理(如果有的话)。
For analogy think of pizza outlet, if only one person is taking order and handing over the order to kitchen, once its ready cutting it, packing and giving it to customer. Where ever there is wait, he just moves on to next task. This is what node does, that person wont hang-on next to kitchen until pizza gets cooked.
比方说披萨店,如果只有一个人在做订单,把订单交给厨房,一旦它准备好了,就把它包装好,交给顾客。无论何时有等待,他都会继续下一个任务。这就是node要做的,那个人在披萨被煮熟之前不会挂在厨房旁边。
In case of worker based approach think of a bank teller and you see couple of them (may be 5 or so) they take every kind of request but they dont switch between customer / request.
如果以工作为基础的方法,想象一个银行出纳员,你看到他们中的几个(可能是5个左右)他们接受各种各样的请求,但是他们不会在客户/请求之间切换。
#2
1
Refer to these resources for a deeper understanding of how JavaScript event loop works.
请参阅这些参考资料,以进一步了解JavaScript事件循环是如何工作的。
https://www.youtube.com/watch?v=8aGhZQkoFbQ
https://www.youtube.com/watch?v=8aGhZQkoFbQ
http://latentflip.com/loupe/
#3
0
I can't answer all your doubts, but would like you to have some clarity over AJAX. AJAX - Asynchronous JavaScript + XML is a technique to make requests to a server. Nodejs server knows how to handle such requests, but saying that is the only thing it can do is absolutely wrong. Nodejs is single threaded, hence async. Whether it is good for CPU intensive tasks, I would say why not, unless you want to solve issues in a multithreaded fashion.
我无法回答你所有的疑问,但希望你能对AJAX有所了解。AJAX -异步JavaScript + XML是向服务器发出请求的一种技术。Nodejs服务器知道如何处理此类请求,但说这是它惟一能做的事情是绝对错误的。Nodejs是单线程的,因此是异步的。不管它是否适合CPU密集型任务,我想说为什么不,除非您希望以多线程的方式解决问题。