这是什么意思是说Apache生成每个请求的线程,但Node.js的不? [英] What does it mean to say Apache spawns a thread per request, but node.js does not?

查看:127
本文介绍了这是什么意思是说Apache生成每个请求的线程,但Node.js的不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过的Node.js和其他服务器如Apache,那里的线程是不同的。我根本不明白线程的意思。

I have read about node.js and other servers such as Apache, where the threading is different. I simply do not understand what the threading means.

如果我有一个运行SQL打数据库中的网页,说,在一个服务器端页面三种不同的数据库,这是什么意思对Node.js的线程? Apache的?什么是线程的意思是在这里吗?

If I have a webpage that runs SQL to hit a database, say three different databases in the one server side page, what does that mean for threading in node.js? Apache? What does "thread" mean here?

或者作为一个文章我看了,开始一个新的线程来处理每个请求。

Or as an article I saw, "start a new thread to handle each request."

这是什么意思是说Apache生成每个请求的线程,但Node.js的不?

What does it mean to say Apache spawns a thread per request, but node.js does not?

编辑:我希望我能把握的例子。我已经习惯了有击中一个数据库(S)的服务器端页面。该文件中的几个连接。

I am hoping for an example that I can grasp. I'm used to having a server side page that hits a database(s). Several connections inside that file.

推荐答案

一个线程是程序执行的环境。程序是单线程只能做一件事一次,在多线程程序可以同时做很多事情。

A thread is a context of program execution. Programs that are single-threaded can only do one thing at once, where multi-threaded programs can do many things at once.

认为它像在一家餐厅的厨房。一个单一的厨师能真正做到只有一个任务的时间,是切碎的意见,或将东西烤箱。如果订单来在需要大量从厨师的工作(如制作沙拉与把东西放在烤箱和等待)一些餐饮可能被延迟,因为厨师忙。在另一方面,如果师傅只是有烤了一堆东西,没有为他做的,他可以​​让其他餐点,同时在烤箱等待食物很多工作要做。

Think of it like a kitchen at a restaurant. A single chef can really only do one task at a time, be that chopping opinions or putting something in an oven. If an order comes in that requires lots of work from the chef (such as making salads vs. putting stuff in the oven and waiting) some meals may get delayed because that chef is busy. On the other hand, if that chef just has to bake a bunch of stuff, there isn't much work for him to do and he can make other meals while waiting for the food in the oven to be done.

通过多个厨师,其中许多任务可以同时进行。多餐可以同时ppared $ P $。

With multiple chefs, many of these tasks can be done simultaneously. Many meals can be prepared simultaneously.

Apache的线程模型,就像雇厨师固定数量(无论你restauarant有多少客户,当晚),每个厨师只能在一个时间上的一餐工作。这意味着,如果一个点餐进来,有专门的厨师被分配到一顿。将有时间,当该厨师正忙着剁配料和混合蛋糕面糊,但也会有时间的时候,他只是站在那里等着土豆煮。在任何时候,你可以有你的大部分厨师闲置,等待土豆煮和蛋糕烘烤并没有更多的订单将在工作,因为每个厨师在一个时间奉献给一个数量级。

Apache's threading model is like hiring a fixed number of chefs (regardless of how many customers your restauarant has that night) and each chef can only work on one meal at a time. That means that if a meal order comes in, a dedicated chef is assigned to that meal. There will be times when that chef is busy chopping up ingredients and mixing cake batter, but there will also be times when he's just standing around waiting for the potatoes to boil. At any given time, you could have most of your chefs sitting idle, waiting on potatoes to boil and cake to bake and no more orders will be worked on, since each chef is dedicated to one order at a time.

要更糟糕的是,你的厨房只是一样大,你能负担得起做出来。每个厨师占用空间和资源,你可能在这里一帮厨师站在旁边拿着唯一可用的勺子是$ P $从得到的食物作出pventing其他厨师的局面。

To make matters worse, your kitchen is only as big as you can afford to make it. Each chef takes up space and resources, and you may have a situation where a bunch of chefs standing around holding the only spoons available are preventing other chefs from getting their food made.

Nginx的是,你并没有问其他Web服务器(通常用来作为代理),但我包括它来解释另一个线程模型。它还聘请厨师固定数量,但他们雇用更少。每个厨师可以同时在多个餐工作。所以,如果他们正在等待土豆沸腾,同时订单进来一个切碎的沙拉,他们可以对沙拉,而不是周围的闲置站着去工作。可以有一个更小的厨房(相对于客户的餐厅/数的大小),并获得相同数目的膳食出来,或更多。这是一个紧张的剧组是在不浪费时间和资源有效。

Nginx is another web server (often used as a proxy) that you didn't ask about, but I'm including it to explain another threading model. It also hires a fixed number of chefs, but it hires fewer of them. Each chef can work on multiple meals at a time. So, if they're waiting on potatoes to boil while an order comes in for a chopped salad, they can go work on that salad instead of standing around idle. You can have a smaller kitchen (relative to the size of restaurant/number of customers) and get the same number of meals out, or more. It's a tight crew that is effective at not wasting time and resources.

Node.js的是一个有点不同。从JavaScript的角度来看,它是单线程的,但像磁盘和网络IO等任务都在单独的线程自动处理。这就像只有一个厨师的厨房,但是这是有道理的在某些情况下。如果你的厨房有很多为厨师工作很忙,或许是有意义的雇佣更多的厨师做的工作。 (要做到这一点在Node.js的,你只能产生更多的进程,这实际上是像建设旁边对方一帮小厨房,你可以有一个家伙站在了前面协调订单,所有这些厨房)。但是如果你只是一个面包店(主要是刚IO,与厨师有点忙工作),也许你只需要一个厨师。

Node.js is a bit different. It is single-threaded from a JavaScript perspective, but other tasks like disk and network IO are handled on separate threads automatically. It's like having a kitchen with only one chef, but that makes sense in some cases. If your kitchen has a lot of busy work for that chef, perhaps it makes sense to hire more chefs to do work. (To do this in Node.js, you can only spawn more processes, which is effectively like building a bunch of small kitchens right next to each other. You can have one guy standing out front coordinating the orders for all those kitchens.) However, if you're just a bakery (mainly just IO, with little busy-work for the chef), maybe you only need one chef.

要总结这一切起来,不同的线程模型被用来划分工作,并有效地处理它。这线程模型是有道理的取决于你的需要,服务器的其它特性你选择。

To sum all this up, different threading models are used to divide work and process it effectively. Which threading model makes sense depends on your needs, and the other characteristics of the server you are choosing.

这篇关于这是什么意思是说Apache生成每个请求的线程,但Node.js的不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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