简体   繁体   English

NodeJS createServer httpRequest 和线程

[英]NodeJS createServer httpRequest and threads

This is a strange question, but I lack any intuition about how this should work and it is difficult to replicate:这是一个奇怪的问题,但我对这应该如何工作缺乏任何直觉,而且很难复制:

I have a simple web server in NodeJS where I am globally exposing the request that is created:我在 NodeJS 中有一个简单的 Web 服务器,我在其中全局公开创建的请求:

http.createServer(function(req, res) {
    global.request = req;

    // do something... processRequest();
});

Inside of the "do something," is a function which uses the globalized request:在“做某事”内部,是一个使用全球化请求的函数:

function processRequest() {
    var request = global.request;
}

Is it possible for a collision to occur between any two instances of global.request? global.request?任意两个实例之间是否可能发生冲突global.request?

As I understand it, each connection uses a different thread, pooled in a C worker pool, which would mean the collision was impossible (unless the global context was available across said workers, and such sharing across threads seems difficult if not impossible), but because JavaScript is the vehicle for the connections to be processed, I have a sneaking suspicion that such a collision could be possible.据我了解,每个连接使用不同的线程,集中在 C 工作池中,这意味着不可能发生冲突(除非global上下文在所述工作人员之间可用,并且这种跨线程共享似乎很困难,如果不是不可能的话),但是因为 JavaScript 是处理连接的工具,所以我暗中怀疑这种碰撞是可能发生的。

Any help or pointers on how I can improve this question greatly appreciated.非常感谢有关如何改进此问题的任何帮助或指示。

Is it possible for a collision to occur between any two instances of global.request? global.request 的任意两个实例之间是否可能发生冲突?

Yes, a collision can occur but there are not two instances of global.request .是的,可能会发生冲突,但global.request没有两个实例。 There is only one global state in node.js. node.js 中只有一种global状态。

As I understand it, each connection uses a different thread, pooled in a C worker pool据我了解,每个连接使用不同的线程,汇集在 C 工作池中

This is not correct.这是不正确的。 Node.js Javascript is single threaded and all requests run one at a time (or sometimes interleaved), but only on a single thread. Node.js Javascript 是单线程的,所有请求一次运行一个(或有时交错运行),但只在单个线程上运行。 node.js does not use individual threads for TCP connections. node.js 不使用单独的线程进行 TCP 连接。 One thread is used for all networking.一个线程用于所有网络。 There is a C++ thread pool that is used for some disk I/O operations, but the Javascript side of things is still single threaded.有一个 C++ 线程池用于一些磁盘 I/O 操作,但 Javascript 方面仍然是单线程的。 The I/O threads are just used to implement an asynchronous behavior which is surfaced through Javascript. I/O 线程仅用于实现通过 Javascript 呈现的异步行为。 Those I/O threads are not related to the networking implementation, but may be part of your confusion in understanding.这些 I/O 线程与网络实现无关,但可能是您理解混乱的一部分。

In node.js, multiple requests can be "in-flight" at the same time.在 node.js 中,多个请求可以同时“进行中”。 That means that ANY data which can be accessed by requests needs to be aware of this.这意味着请求可以访问的任何数据都需要注意这一点。

Data can be shared among requests in globally accessible variables, but you cannot store request-specific data in a global because other requests may trounce it.数据可以在全局可访问变量中的请求之间共享,但您不能将特定于请求的数据存储在全局中,因为其他请求可能会击败它。

Node.js Javascript is single threaded which means that a given request will not be pre-emptively interrupted while it is running which simplifies things a lot, but as soon as a request starts an async operation and then finishes it's current thread of execution (waiting for the async callback to be called), then other requests can run. Node.js Javascript 是单线程的,这意味着给定的请求在运行时不会被抢先中断,这大大简化了事情,但是一旦请求开始异步操作,然后完成它的当前执行线程(等待调用异步回调),然后其他请求可以运行。 Thus, you can have multiple requests in-flight at the same time.因此,您可以同时处理多个请求。

The usual solution for an issue like your code shows it to NOT store request-specific things in globals at all.像您的代码这样的问题的常用解决方案表明,它根本不将特定于请求的内容存储在全局变量中。 Instead, you pass the request/response arguments down to any functions that need to use them.相反,您将请求/响应参数传递给任何需要使用它们的函数。 You can store other types of data on the request or response objects themselves too.您也可以在请求或响应对象本身上存储其他类型的数据。


So, instead of this:所以,而不是这个:

http.createServer(function(req, res) {
    global.request = req;

    // do something... processRequest();
});

function processRequest() {
    var request = global.request;
}

You must pass the request down to any function that needs it:您必须将请求传递给任何需要它的函数:

http.createServer(function(req, res) {

    // do something... 
    processRequest(req);
});

function processRequest(req) {
    // operate on the request here
}

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

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