简体   繁体   English

当许多请求同时到达Web服务器时,如何工作? 诺德斯

[英]How work when a lot of requests arrive at the web server at the same time? NODEJS

IN NODEJS: IF we can only run one function at the same time if node is not using multiple threads. 在NODEJS中:如果节点不使用多个线程,那么我们只能同时运行一个函数。 How can this work when a lot of requests arrive at the web server at the same time? 当许多请求同时到达Web服务器时,该如何工作?

Can to clear the panorama about thread and process?? 可以清除有关线程和进程的全景吗?

Theoretically if a large number of requests happened in the same second - or if each request has to do something that takes a while, like hard math - your server could get bogged down. 从理论上讲,如果大量请求在同一秒内发生-或每个请求都需要花费一些时间(例如硬数学),则服务器可能会陷入瘫痪。 Either by not responding to people at the "end" of the line (late by milliseconds), or never finishing all of the things Node needs to do to serve those requests at the "front" of the line. 要么不响应热线“末端”的人员(以毫秒为单位),要么永不完成Node所需要做的所有事情,以响应热线“末端”的那些请求。

In general the strategy Node takes is that if you're going to perform a long operation - like querying the database - the execution of the program should not sit around waiting, but should "call back" to some other function when the database query is eventually done. 总的来说,Node采取的策略是,如果您要执行长时间的操作(例如查询数据库),则程序的执行不应该处于等待状态,而应在数据库查询到时“回调”其他功能。最终完成了。

I talk more about this in another SO answer . 我在另一个SO答案中进一步讨论了这一点。 You could Google "node.js is cancer" for other examples of just what you are talking about. 您可以使用Google“ node.js致癌”获取有关您正在谈论的内容的其他示例。

But the prevalence of this strategy is one of the major differences between Node and other languages/frameworks: that's just how Javascript deals. 但是这种策略的普遍存在是Node与其他语言/框架之间的主要区别之一:这就是Javascript的处理方式。

Now, in practice, several things actually happen. 现在,在实践中,实际上发生了几件事。

First, any production Node app really should be running with Cluster or some kind of solution that provides load balancing. 首先,任何生产节点应用程序实际上都应该与群集或提供负载平衡的某种解决方案一起运行。 Because you'd be having multiple processes of your app working, your solution can do more than one thing at once. 由于您的应用程序要经历多个流程,因此您的解决方案可以一次完成多个任务。

Secondly, in general Node.js stays up really well, because the idea of not waiting around for everything. 其次,一般而言,Node.js保持良好状态,因为不等待所有内容的想法。 It keeps your server busy, instead of cooling it's jets waiting for something to be done. 它使您的服务器保持繁忙,而不是冷却喷气机,等待完成某件事。

Thirdly, yes you do have to be careful about what you do in the server. 第三,是的,您必须小心在服务器中执行的操作。 If something's going to take too long (modiying all the records in the database), probably wise to do it in the background via some kind of worker queue system: "Hey, I need to update this person's username in all of (these) records in the database" probably should happen by yet another Node.js process being the worker. 如果某件事将花费太长时间(修改数据库中的所有记录),则明智的做法是通过某种类型的工作人员队列系统在后台执行此操作:“嘿,我需要在所有(这些)记录中更新此人的用户名在数据库中”可能应该由另一个Node.js进程作为工作进程来发生。

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

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