简体   繁体   English

是否像PHP一样,node.js或django是“请求驱动”的?

[英]Is node.js or django “request driven” like PHP?

To be honest I'm a Linux admin and haven't done web development in a few years. 老实说,我是一名Linux管理员,并且几年没有做过Web开发。 My tool of choice for making anything web of the past 10+ years has been LAMP. 在过去的10多年中,我制作任何网络的首选工具是LAMP。

I'm working on a REST API and I'm thinking of branching out. 我正在开发REST API,并且正在考虑进行扩展。 There are ways to do REST in PHP I know but maybe it's not the best way anymore. 我知道有很多方法可以在PHP中执行REST,但也许这不再是最好的方法。

The PHP model as far as I'm concerned goes like this: 就我而言,PHP模型是这样的:

  • User makes request in their browser (or an AJAX call) 用户在其浏览器中发出请求(或AJAX调用)
  • PHP get's called up. PHP调用了。
  • PHP starts at the top and works it's way sequentially to the bottom, connecting to the DB, running queries, doing stuff, and outputting data. PHP从顶部开始,然后按顺序从底部开始,连接到数据库,运行查询,执行操作并输出数据。
  • Request is over, the user has their data and is ready to make a new one. 请求结束,用户拥有他们的数据,并准备制作一个新数据。

With PHP each request is a new deal. 使用PHP,每个请求都是一笔新交易。 There are no shared variables (you can pass things with cookies\\sessions) and once the script is done it is done. 没有共享变量(可以通过cookie \\ session传递内容),一旦脚本完成就完成了。

While reading about Node's architecture it seems as if everything runs in this single threaded event loop. 在阅读有关Node的体系结构时,似乎一切都在此单线程事件循环中运行。 Does that mean node is "always running" and you can share variables between requests or is there a new event loop per request? 这是否意味着节点“始终在运行”并且您可以在请求之间共享变量,还是每个请求都有新的事件循环? Does that mean you can write sloppy code to block node from accepting new requests from other random users? 这是否意味着您可以编写草率的代码来阻止节点接受其他随机用户的新请求? Does Django operate similar? Django操作类似吗? If that is the case is it possible to spawn new node processes per request? 如果是这样,是否有可能根据每个请求生成新的节点进程?

That seems kind dangerous. 这似乎很危险。 In PHP you can write sloppy code that causes the single request to run slow but each request is kind of independent (system resources permitting). 在PHP中,您可以编写草率的代码,使单个请求运行缓慢,但是每个请求都是独立的(系统资源允许)。 I also like the "do script and terminate" nature of PHP but maybe that is just an old school way of thinking. 我也喜欢PHP的“执行脚本并终止”的性质,但这也许只是一种古老的思维方式。

I'm going to have a phone app\\website that passes data back and forth to an API to get information from a database (think an inventory management tool). 我将拥有一个电话应用程序\\网站,该网站将数据来回传递到API以从数据库中获取信息(请考虑使用库存管理工具)。 I have clearly defined API calls and a front end dev just calls them as needed. 我已经明确定义了API调用,并且前端开发人员仅在需要时调用它们。 I have various calls to get various bits of data. 我有各种各样的电话来获取各种各样的数据。

All code works exactly the way you describe, because this is how the (traditional) web works. 所有代码都完全按照您描述的方式工作,因为这是(传统)网络的工作方式。 Request leads to a response. 请求导致响应。 There is no context shared between requests. 请求之间没有共享的上下文。 Each request is blocking on the client. 每个请求都在客户端上被阻止。

It works the same way for PHP, for Django and for everyone else since the days of CGI. 自CGI诞生以来,它对于PHP,Django和其他所有人的工作方式相同。

The difference is how the web server delegates these requests to the backend code. 区别在于Web服务器如何将这些请求委派给后端代码。 You have different models here; 您在这里有不同的型号; some involve multiple threads, others involve multiple processes of the backend code. 一些涉及多个线程,另一些涉及后端代码的多个进程。

The only thing that is "always running" is the web server (or whatever process you have bound to a socket listening on port 80 in an infinite loop). 唯一“始终在运行”的东西是Web服务器(或绑定到套接字的套接字无限循环监听的任何进程)。 Everything else is waiting for a request to come in. 其他所有内容都在等待请求的到来。

Node is a runtime envrionment; 节点是运行时环境; you typically develop applications to run on top of node using a framework, such as express . 您通常会使用诸如express的框架来开发在节点顶部运行的应用程序。

If you look at the basic hello world of express, you'll notice it runs an infinite loop listening for requests: 如果您看一下express的基本问候世界,您会发现它运行无限循环来监听请求:

const express = require('express')
const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

In the node world, there is one main event loop - but all i/o happens in separate non-blocking threads. 在节点世界中,有一个主事件循环-但是所有的I / O都发生在单独的非阻塞线程中。

Each event is put on a queue, and the main loop processes things from this queue (image from the excellent software engineering daily ): 每个事件都放在一个队列中,主循环处理该队列中的内容( 每日优秀软件工程的图片):

在此处输入图片说明

Therefore, you have to be careful when writing node applications because if you accidentally write a lot of blocking code - it will have a drastic impact on the rest of the application - unlike other architectures where you can get away with a bit of sloppiness since each process is separate and isolated. 因此,在编写节点应用程序时必须小心,因为如果不小心编写了很多阻塞代码-这将对应用程序的其余部分产生巨大影响-与其他体系结构不同,在这种体系结构中您可能会有些草率。流程是独立且孤立的。

I dont know about node (since I use python only) but I do know that DJANGO is request driven. 我不知道节点(因为我仅使用python),但我确实知道DJANGO是请求驱动的。 (request driven I assume means that you must handle a request properly when stating a view function or else youll get an error.) also isn't this a little too broad (我认为是请求驱动的,这意味着您在声明视图函数时必须正确处理请求,否则会出现错误。)这还不是太宽泛

While reading about Node's architecture it seems as if everything runs in this single threaded event loop. 在阅读有关Node的体系结构时,似乎一切都在此单线程事件循环中运行。 Does that mean node is "always running" and you can share variables between requests or is there a new event loop per request? 这是否意味着节点“始终在运行”并且您可以在请求之间共享变量,还是每个请求都有新的事件循环?

Yes, node.js is a always running single thread. 是的,node.js是一个始终运行的单线程。 Its a loop that handles your requests and sends out responses. 它是一个处理您的请求并发出响应的循环。 Compared to a LAMP setup: node.js is apache and php together. 与LAMP设置相比:node.js是apache和php在一起。

Does that mean you can write sloppy code to block node from accepting new requests from other random users? 这是否意味着您可以编写草率的代码来阻止节点接受其他随机用户的新请求?

Yes, it does. 是的,它确实。 You can share states between requests and you can block other requests, even worse you can kill the process completely, or display some personal data to other users. 您可以在请求之间共享状态,也可以阻止其他请求,甚至更糟的是,您可以完全终止该过程,或向其他用户显示一些个人数据。 There are quite a few ways to shoot yourself in the foot with node.js. 有很多方法可以使用node.js步履蹒跚。 But on the other hand you can write terrific efficient applications in node.js and you can gain a lot of performance from the fact that you don't need to restart your whole app for every single request. 但是另一方面,您可以在node.js中编写出色的高效应用程序,并且由于不需要为每个单个请求重新启动整个应用程序,因此可以获得很多性能。

Does that mean you can write sloppy code to block node from accepting new requests from other random users? 这是否意味着您可以编写草率的代码来阻止节点接受其他随机用户的新请求?

Yup try, this : 是的,这:

const express = require('express')
const app = express()

app.get('/', function (req, res) {
    while(true){
    }
  res.send('Hello World!')
})
app.get('/ok', function (req, res) {
  res.send('ok!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

Access /ok , no biggie. 访问/ok ,没什么大不了的。 Then access / which won't give you any answer back, then in another tab access /ok again, you can't. 然后访问/不会给您任何答案,然后在另一个选项卡中再次访问/ok则不能。 Node is still in the loop of / , blocking other users. Node仍然处于/循环中,阻止了其他用户。

The obvious solution would be to not write sloppy code, but since you mention just starting out it could make you somewhat anxious. 显而易见的解决方案是不编写草率的代码,但是由于您提到刚入门,可能会使您有些焦虑。 IE: you might not know what's good or bad. IE:您可能不知道什么是好是坏。 I'll argue that it is the same in any language. 我会说任何一种语言都是一样的。

So as you asked, can you run it in multiple process ? 因此,如您所问,您可以在多个进程中运行它吗? Yes. 是。

To do so, I guess your imagination is the limit. 为此,我想您的想象力是极限。 For example, if your app has no state (you mentioned REST), you could run multiple servers, each listening on different port and route each request to an individual server. 例如,如果您的应用程序没有状态(您提到了REST),则可以运行多个服务器,每个服务器侦听不同的端口,并将每个请求路由到单个服务器。

There is also NodeCluster , although I don't use it, I guess that's what it helps doing. 还有NodeCluster ,尽管我不使用它,但我想这正是它的用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM