非阻塞HTTP服务器中的阻塞代码 [英] blocking code in non-blocking http server

查看:83
本文介绍了非阻塞HTTP服务器中的阻塞代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个http服务器,它是node.js,因此也是非阻塞的.

For example, I have a http server, which is node.js, so is non-blocking.

对于每个请求都将执行一个DB操作.

For each request will do a DB operation.

我不明白的是,阻塞DB操作和非阻塞DB操作之间有什么区别?

what I cannot understand is that what is the difference between a blocking DB operation and a non-blocking DB operation?

由于Web服务器是非阻塞的,所以请求内的阻塞DB操作没有区别吗?

Since the web server is non-blocking, so a blocking DB operation inside the request makes no difference?

推荐答案

以下是一个可以帮助您理解的类比.假设您正在销售中,今天有50个电话要打.

Here's an analogy that might help you understand. Let's suppose you're in sales and you have 50 phone calls to make today.

在阻止模型中,您拨打电话,并且如果该人不准备与您交谈,则您必须坐在线路上,然后等待,直到他们准备与您交谈.当他们终于准备好与您交谈时,您可以进行通话,挂断电话,然后才能打出下一个电话并重复此过程.因此,电话忙碌且无法拨打电话的时间等于您等待客户端准备与您通话的时间加上您一直通话的时间之和.

In the blocking model, you place a call and, if the person isn't ready to talk to you, you have to sit on the line and you wait and you wait until they are ready to talk to you. When they are finally ready to talk to you, you have your conversation, you hang up and only then can you make your next call and repeat the process. So, the time your phone is busy and you are not able to make calls is the sum of all the time you were waiting for the client to be ready to talk to you plus all the time you talked.

在非阻塞模式中,您拨打电话,如果此人无法立即与之通话,则您会留下一条快速消息,当他们准备好通话时,他们会给您回电.您挂断了留言,然后立即致电您的下一个客户.手机占线的总时间仅是留言时间,然后是他们回叫时的通话时间.实际上,他们在一分钟或三个小时内给您回电对您来说并不重要-这不会影响您拨打电话的整体效率.显然,您可以使用非阻塞模型发出并完成更多呼叫,因为您并没有在浪费大量时间来等待客户端准备好进行其他事情.

In the non-blocking model, you place a call and if the person is not immediately available, you leave a quick message and they call you back when they are ready to talk. You hang up from leaving the message and immediately call your next client. The total time your phone is busy is only the time to leave a quick message and then the time you talk when they call back. It doesn't actually matter to you if they call you back in one minute or three hours - it does not affect your overall efficiency at making calls. You can obviously place and complete a lot more calls with the non-blocking model because you aren't wasting a lot of time when you could be doing something else by just waiting for the client to be ready to talk.

node.js通过使用非阻塞I/O模型而不使用线程来提高效率.如果实施得当,此非阻塞模型比拥有许多正在等待阻塞请求的单独线程要有效.

node.js gets its efficiency by using the non-blocking I/O model and not using threads. Implemented appropriately, this non-blocking model is more efficient than having lots of individual threads that are all waiting on their blocking requests.

因此,在node.js中,如果您有阻止的数据库请求,则在整个数据库请求被阻止的整个时间内,node.js Web服务器无法执行其他任何操作.它只有一个线程,因此,如果该线程忙于一个阻塞请求,而该阻塞请求需要一段时间才能完成服务器,则基本上会在相当长的一段时间内使它无用.这将是对node.js服务器进程的破坏性实现,并且以这种方式使用node.js毫无意义.

So, in node.js if you have a blocking database request, then the entire time that database request is blocking, the node.js web server cannot do anything else. It only has one thread so if that thread is busy with a blocking request that takes awhile to complete the server is basically rendered useless for a significant period of time. This would be a broken implementation of a node.js server process and there would be little point in using node.js that way.

对于非阻塞数据库请求,node.js服务器开始处理http请求.它运行一些代码,然后到达非阻塞数据库请求.该请求已启动,并且为完成注册了回调,但是由于它是非阻塞的,因此数据库调用会立即返回. node.js服务器返回其事件循环,并可以立即开始处理其他http请求或其他事件以供服务器处理(例如,其他http请求的完成).然后,在某个时间之后,数据库调用完成,并将一个条目放入事件队列中以触发其回调.当nodejs服务器到达事件队列中的该事件时,将调用回调,并且该原始请求现在具有数据库服务器的答案,并且可以完成该原始请求.同时,一直在等待该数据库请求完成时,它一直在忙于处理其他事情-所有这些都只有一个线程.

With a non-blocking database request, the node.js server starts processing an http request. It runs some code, then it gets to the non-blocking database request. That request is started and a callback is registered for completion, but because it's non-blocking, the database call returns immediately. The node.js server returns back to its event loop and can immediately start working on other http requests or other events for the server to process (e.g. the completion of other http requests). Then, sometime later, the database calls finishes and puts an entry in the event queue to trigger its callback. When the nodejs server gets to that event in the event queue, the callback is called and that original request now has the answer from the database server and can finish that original request. Meanwhile, all the time it was waiting for that database request to finish, it was busy processing other things - all with it's one single thread.

这篇关于非阻塞HTTP服务器中的阻塞代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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