After reading about event loops and how async works in node.js, this is my understanding of node.js:
在阅读了关于事件循环以及node.js中异步如何工作之后,这是我对node.js的理解:
- Node actually runs processes one at a time and not simultaneously.
- Node really shines when multiple databse I/O tasks are called. It runs faster (than blocking I/O) because it doesn't wait for the response of one call before dealing with the next call. And while dealing with the other call, when the result of the first call arrives, it "gets back to it", basically going back and forth crossing calls and callbacks, without leaving the OS process idle, as opposed to what blocking I/O does. Please correct me if I'm wrong.
节点实际上一次运行一个进程而不是同时运行进程。
当调用多个数据库I / O任务时,节点真的很闪耀。它运行得更快(比阻塞I / O),因为它在处理下一个调用之前不会等待一个调用的响应。在处理另一个调用时,当第一个调用的结果到来时,它“返回到它”,基本上来回交叉调用和回调,而不会让操作系统进程空闲,而不是阻塞I / O确实。如果我错了,请纠正我。
But here's my question: Non-blocking I/O seems to be faster than blocking I/O only if the entity (server/process/thread?) that handles the request sent by node, is not the node server itself.
但这是我的问题:只有当处理节点发送的请求的实体(服务器/进程/线程?)不是节点服务器本身时,非阻塞I / O似乎比阻塞I / O更快。
What would be the cases when the sever handling the request is the same server making the request? If my first bullet is correct, in this case a blocking I/O will work faster than non-blocking if it uses different threads for the task? Would file compression be an example to such I/O task that works faster on multithreaded blocking I/O?
处理请求的服务器与发出请求的服务器相同时会出现什么情况?如果我的第一个项目符号是正确的,在这种情况下,如果阻塞I / O使用不同的线程来执行任务,那么阻塞I / O的工作速度会比非阻塞更快文件压缩是否是这种I / O任务的一个例子,它在多线程阻塞I / O上运行得更快?
1 个解决方案
#1
2
The main benefit of non-blocking operations is that a relatively heavyweight CPU thread is not kept busy while the server is waiting for something to happen elsewhere (networking, disk I/O, etc...). This means that many different requests can be "in-flight" with only the single CPU thread and no thread is stuck waiting for I/O. A burden is placed back on the developer to write async-friendly code and to use async I/O operations, but in a heavy I/O bound operation, there can be a real benefit to server scalability. The single thread model also really simplifies access to shared resources since there is far, far less opportunity for threading conflicts, deadlocks, etc... This can result in fewer hard-to-find thread synchronization bugs that tend to only nail your server at the worst time (e.g. when it's busy).
非阻塞操作的主要好处是,当服务器等待其他地方发生某些事情时,相对较重的CPU线程不会保持忙碌(网络,磁盘I / O等......)。这意味着许多不同的请求可以在“飞行中”只有单个CPU线程,并且没有线程等待I / O。给开发人员带来了负担,要编写异步友好代码并使用异步I / O操作,但在繁重的I / O绑定操作中,服务器可伸缩性可以带来真正的好处。单线程模型也真正简化了对共享资源的访问,因为线程冲突,死锁等的机会远远少于此......这可以减少难以发现的线程同步错误,这些错误往往只会影响您的服务器最糟糕的时间(例如,当它很忙)。
Yes, non-blocking I/O only really helps if the agent handling the I/O operation is not node.js itself because the whole point of non-blocking I/O in node is that node is free to use its single thread to go do other things while the I/O operation is running and if it's node that is serving the I/O operation then that wouldn't be true.
是的,非阻塞I / O只有在处理I / O操作的代理不是node.js本身时才有用,因为节点中非阻塞I / O的重点是该节点可以*使用其单线程在I / O操作正在运行时执行其他操作,如果它是为I / O操作提供服务的节点,那么这将不是真的。
Sorry, but I don't understand the part of your question about file compression. File compression takes a certain amount of CPU, no matter who handles it and there are a bunch of different considerations if you were trying to decide whether to handle it inside of node itself or in an outside process (running a different thread). That isn't a simple question. I'd probably start with using whatever code I already had for the compression (e.g. use node code if that's what you had or an external library/process if that's what you had) and only investigate a different option if you actually ran into a performance or scalability issue or knew you had an issue.
对不起,但我不明白你关于文件压缩的部分问题。文件压缩需要一定数量的CPU,无论谁处理它,如果你试图决定是在节点本身内部还是在外部进程(运行不同的线程)中处理它,那么会有很多不同的考虑因素。这不是一个简单的问题。我可能会开始使用我已经用于压缩的任何代码(例如,使用节点代码,如果这是您拥有的代码或外部库/进程,如果这就是您所拥有的),并且如果您实际遇到性能,则仅调查其他选项或可伸缩性问题或知道您有问题。
FYI, a simple mechanism for handling compression would be to spool the uncompressed data to files in a temporary directory from your node.js app and then have another process (which could be written in any system, even include node) that just looks for files in the temporary directory to which it applies the compression and then does something more permanent with the resulting compressed data.
仅供参考,一种处理压缩的简单机制是将未压缩的数据从您的node.js应用程序假脱机到临时目录中的文件,然后让另一个进程(可以在任何系统中编写,甚至包括节点)只查找文件在它应用压缩的临时目录中,然后使用生成的压缩数据执行更永久的操作。
#1
2
The main benefit of non-blocking operations is that a relatively heavyweight CPU thread is not kept busy while the server is waiting for something to happen elsewhere (networking, disk I/O, etc...). This means that many different requests can be "in-flight" with only the single CPU thread and no thread is stuck waiting for I/O. A burden is placed back on the developer to write async-friendly code and to use async I/O operations, but in a heavy I/O bound operation, there can be a real benefit to server scalability. The single thread model also really simplifies access to shared resources since there is far, far less opportunity for threading conflicts, deadlocks, etc... This can result in fewer hard-to-find thread synchronization bugs that tend to only nail your server at the worst time (e.g. when it's busy).
非阻塞操作的主要好处是,当服务器等待其他地方发生某些事情时,相对较重的CPU线程不会保持忙碌(网络,磁盘I / O等......)。这意味着许多不同的请求可以在“飞行中”只有单个CPU线程,并且没有线程等待I / O。给开发人员带来了负担,要编写异步友好代码并使用异步I / O操作,但在繁重的I / O绑定操作中,服务器可伸缩性可以带来真正的好处。单线程模型也真正简化了对共享资源的访问,因为线程冲突,死锁等的机会远远少于此......这可以减少难以发现的线程同步错误,这些错误往往只会影响您的服务器最糟糕的时间(例如,当它很忙)。
Yes, non-blocking I/O only really helps if the agent handling the I/O operation is not node.js itself because the whole point of non-blocking I/O in node is that node is free to use its single thread to go do other things while the I/O operation is running and if it's node that is serving the I/O operation then that wouldn't be true.
是的,非阻塞I / O只有在处理I / O操作的代理不是node.js本身时才有用,因为节点中非阻塞I / O的重点是该节点可以*使用其单线程在I / O操作正在运行时执行其他操作,如果它是为I / O操作提供服务的节点,那么这将不是真的。
Sorry, but I don't understand the part of your question about file compression. File compression takes a certain amount of CPU, no matter who handles it and there are a bunch of different considerations if you were trying to decide whether to handle it inside of node itself or in an outside process (running a different thread). That isn't a simple question. I'd probably start with using whatever code I already had for the compression (e.g. use node code if that's what you had or an external library/process if that's what you had) and only investigate a different option if you actually ran into a performance or scalability issue or knew you had an issue.
对不起,但我不明白你关于文件压缩的部分问题。文件压缩需要一定数量的CPU,无论谁处理它,如果你试图决定是在节点本身内部还是在外部进程(运行不同的线程)中处理它,那么会有很多不同的考虑因素。这不是一个简单的问题。我可能会开始使用我已经用于压缩的任何代码(例如,使用节点代码,如果这是您拥有的代码或外部库/进程,如果这就是您所拥有的),并且如果您实际遇到性能,则仅调查其他选项或可伸缩性问题或知道您有问题。
FYI, a simple mechanism for handling compression would be to spool the uncompressed data to files in a temporary directory from your node.js app and then have another process (which could be written in any system, even include node) that just looks for files in the temporary directory to which it applies the compression and then does something more permanent with the resulting compressed data.
仅供参考,一种处理压缩的简单机制是将未压缩的数据从您的node.js应用程序假脱机到临时目录中的文件,然后让另一个进程(可以在任何系统中编写,甚至包括节点)只查找文件在它应用压缩的临时目录中,然后使用生成的压缩数据执行更永久的操作。