当这些非阻塞I/O服务器的单个请求花费很长时间时,会发生什么情况? [英] What happens when a single request takes a long time with these non-blocking I/O servers?

查看:90
本文介绍了当这些非阻塞I/O服务器的单个请求花费很长时间时,会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Node.js,eventlet或任何其他非阻塞服务器,当给定请求花费很长时间时,会阻塞所有其他请求吗?

With Node.js, or eventlet or any other non-blocking server, what happens when a given request takes long, does it then block all other requests?

例如,一个请求进入,需要200ms的时间进行计算,这将阻止其他请求,例如nodejs使用单个线程.

Example, a request comes in, and takes 200ms to compute, this will block other requests since e.g. nodejs uses a single thread.

平均每秒15K会下降,这是因为计算给定请求的响应所花费的实际时间.

Meaning your 15K per second will go down substantially because of the actual time it takes to compute the response for a given request.

但是这对我来说似乎是错的,所以我问真正发生了什么,因为我无法想象这是如何工作的.

But this just seems wrong to me, so I'm asking what really happens as I can't imagine that is how things work.

推荐答案

是否阻止"取决于您对阻止"的定义.通常,阻塞意味着您的CPU本质上处于空闲状态,但是当前线程无法执行任何操作,因为它正在等待I/O等.除非您使用非推荐的同步I/O函数,否则在node.js中这种事情通常不会发生.取而代之的是,函数会快速返回,并且当它们开始执行I/O任务时,您的回调将被调用,然后从那里进行调用.在此期间,可以处理其他请求.

Whether or not it "blocks" is dependent on your definition of "block". Typically block means that your CPU is essentially idle, but the current thread isn't able to do anything with it because it is waiting for I/O or the like. That sort of thing doesn't tend to happen in node.js unless you use the non-recommended synchronous I/O functions. Instead, functions return quickly, and when the I/O task they started complete, your callback gets called and you take it from there. In the interim, other requests can be processed.

如果您要在节点上进行大量计算,那么在完成该操作之前,将无法使用CPU,但是有一个非常不同的原因:CPU实际上很忙.通常,这并不是人们说阻止"时的意思,而是一个很长的计算.

If you are doing something computation-heavy in node, nothing else is going to be able to use the CPU until it is done, but for a very different reason: the CPU is actually busy. Typically this is not what people mean when they say "blocking", instead, it's just a long computation.

如果不涉及I/O且纯粹在进行计算,则花费200ms的时间很长.老实说,那可能不是您应该在节点中要做的事情.一种更符合节点精神的解决方案是,在由node调用的另一个(非JavaScript)程序中进行这种数字运算,并在完成后调用您的回调.假设您有一台多核计算机(或其他程序正在另一台计算机上运行),那么当其他程序开始运行时,节点可以继续响应请求.

200ms is a long time for something to take if it doesn't involve I/O and is purely doing computation. That's probably not the sort of thing you should be doing in node, to be honest. A solution more in the spirit of node would be to have that sort of number crunching happen in another (non-javascript) program that is called by node, and that calls your callback when complete. Assuming you have a multi-core machine (or the other program is running on a different machine), node can continue to respond to requests while the other program crunches away.

在某些情况下,集群(如其他人所提到的)可能会有所帮助,但我怀疑您的集群确实是其中之一.集群的真正目的是,当您有大量的小请求,而这些请求的总和超过一个CPU的单个内核所能处理的时,而不是在您的单个请求各自花费数百毫秒的情况下.

There are cases where a cluster (as others have mentioned) might help, but I doubt yours is really one of those. Clusters really are made for when you have lots and lots of little requests that together are more than a single core of the CPU can handle, not for the case where you have single requests that take hundreds of milliseconds each.

这篇关于当这些非阻塞I/O服务器的单个请求花费很长时间时,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆