简体   繁体   English

Node.js 中的流程是什么

[英]What is process in Node.js

I was learning Node.js and came across something called Node.js process.我正在学习 Node.js 并遇到了一个叫做 Node.js 过程的东西。 After some research done I found this statement: process is an object referencing to the actual computer process running a Node program and allows for access to command-line arguments and much more.在完成一些研究后,我发现了以下语句: process is an object referencing to the actual computer process running a Node program and allows for access to command-line arguments and much more. here https://www.codecademy.com/articles/what-is-node .这里https://www.codecademy.com/articles/what-is-node So, the question is "Is it true that process is something within which our Node.js is run and process itself represents processor of a computer?"所以,问题是“进程是否真的是我们的 Node.js 在其中运行并且进程本身代表计算机的处理器的东西?”

Each program running on a computer represents a process.计算机上运行的每个程序都代表一个进程。 It's a top level task that an operating system such as Windows or Linux uses to encapsulate a running program.这是 Windows 或 Linux 等操作系统用来封装正在运行的程序的顶级任务。 Among other things, a process contains:除其他外,一个过程包含:

  1. Code that is running正在运行的代码
  2. Memory that is allocated to it by the OS操作系统分配给它的 Memory
  3. Files or sockets that it has open它打开的文件或 sockets
  4. One or more threads running within the process在进程中运行的一个或多个线程

When a process exits (or crashes), the operating system automatically cleans up resources owned by that process (closes files/sockets, returns memory back to the OS, shuts-down threads, etc...).当进程退出(或崩溃)时,操作系统会自动清理该进程拥有的资源(关闭文件/套接字,将 memory 返回给操作系统,关闭线程等)。

The operating system shares the CPU cores on the computer among all the different processes and threads in those processes running on the computer.操作系统在计算机上运行的这些进程中的所有不同进程和线程之间共享计算机上的 CPU 内核。 In this way, even if lots of programs are all trying to use the CPU at once, they all get some of the CPU time and all appear to be making forward progress.这样,即使很多程序都试图同时使用 CPU,它们也都获得了一些 CPU 时间,并且似乎都在向前推进。 In reality, one gets to run for a little bit of time, then the next, then the next and so on, but those time slices can be so small that they all appear to be running together.实际上,一个人会运行一段时间,然后是下一个,然后是下一个,以此类推,但这些时间片可能非常小,以至于它们看起来都在一起运行。

The term process is an operating system term and not a node.js term.术语进程是操作系统术语,而不是 node.js 术语。 The process module in node.js is a central place where the designers of node.js put a bunch of methods that relate to the overall process such as process.exit() which exits the application and thus stops the process or process.env which gives you access to the environment variables for your program or process.argv which gives you access to the command line arguments your process was started with and so on... These are all things that apply to your overall program running. node.js 中的process模块是 node.js 的设计者放置一系列与整个进程相关的方法的中心位置,例如process.exit()退出应用程序并因此停止进程或process.env让您访问您的程序或process.argv的环境变量,它使您可以访问命令行 arguments 您的进程启动等等......这些都是适用于整个程序运行的所有内容。

First, you need to understand node is a program and it is a host environment for running javascript.首先,您需要了解 node 是一个程序,它是运行 javascript 的主机环境。 You can then run node from the command line like this to execute the program:然后,您可以像这样从命令行运行node来执行程序:

$ node hello.js
Hello world

If you run node without giving it a file, it provides you with a prompt at which you can type JavaScript code and immediately see the result.如果你在没有给它一个文件的情况下运行node ,它会为你提供一个提示,你可以在其中输入 JavaScript 代码并立即看到结果。

$ node
> 1 + 1
2
> [-1, -2, -3].map(Math.abs)
[1, 2, 3]
> process.exit(0)
$

The process binding is available globally in Node. process绑定在 Node.js 中全局可用。 It provides various ways to inspect and manipulate the current program.它提供了各种检查和操作当前程序的方法。 The exit method ends the process and can be given an exit status code, which tells the program that started node (in this case, the command line shell) whether the program completed successfully (code zero) or encountered an error (any other code). exit方法结束进程并且可以给出一个退出状态码,它告诉启动node的程序(在这种情况下,命令行shell)程序是成功完成(代码零)还是遇到错误(任何其他代码) .

process.env allows you to access the environment of node and process.argv allows you to access the arguments passed to the node command. process.env允许您访问node的环境,而process.argv允许您访问传递给node命令的 arguments。 For example, if showargv.js contains the statement console.log(process.argv) , you could run it like this:例如,如果showargv.js包含语句console.log(process.argv) ,您可以像这样运行它:

$ node showargv.js one --and two
["node", "/tmp/showargv.js", "one", "--and", "two"]

The term "process", indeed, is not only a node term but also an "operating system" term, as mentioned by @jfriend00.正如@jfriend00 所提到的,术语“进程”确实不仅是一个node术语,也是一个“操作系统”术语。 Each program has its processes.每个程序都有它的进程。 It is important to remember that the process in node allows users to obtain node related info and have some functions to control the node 's behaviour.重要的是要记住 node 中的process允许用户获取node相关信息并具有一些功能来控制node的行为。

The process module in node.js is a central place where the designers of node.js put a bunch of methods that relate to the overall process. node.js 中的流程模块是 node.js 的设计者放置一堆与整个流程相关的方法的中心位置。 @jfriend00 @jfriend00

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

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