Apache 为每个请求生成一个线程,而 node.js 没有,这是什么意思? [英] What does it mean to say Apache spawns a thread per request, but node.js does not?

查看:18
本文介绍了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 中的线程意味着什么?阿帕奇?这里的线程"是什么意思?

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?

我希望有一个我能理解的例子.我习惯于有一个访问数据库的服务器端页面.该文件中的多个连接.

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 onions 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.

如果有多个厨师,其中许多任务可以同时完成.可以同时准备多餐.

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

Apache 的线程模型就像雇用固定数量的厨师(无论您的餐厅当晚有多少顾客),每个厨师一次只能处理一顿饭.这意味着,如果有订餐,则会为该餐分配一名专门的厨师.有时那个厨师会忙着切食材和搅拌蛋糕糊,但也有时他只是站在原地等待土豆煮沸.在任何给定的时间,您都可以让您的大多数厨师闲置,等待土豆煮沸和蛋糕烘烤,并且不会再处理任何订单,因为每个厨师一次只专注于一个订单.

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.

更糟糕的是,您的厨房只有您能负担得起的大小.每个厨师都占用空间和资源,您可能会遇到这样的情况:一群厨师站在周围,手里拿着唯一可用的勺子,阻止其他厨师制作他们的食物.

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天全站免登陆