-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node.js 子进程:你应该知道的一切 #1838
Node.js 子进程:你应该知道的一切 #1838
Conversation
@sqrthree 校对认领 |
@CACppuccino 好的呢 🍺 |
校对认领 @sqrthree |
@wilsonandusa 妥妥哒 🍻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FrankXiong @sqrthree 校对完毕,请译者注意删除英文,并更正格式问题(如大段的加粗和斜体,及不明符号)
@@ -1,81 +1,134 @@ | |||
> * 原文地址:[Node.js Child Processes: Everything you need to know](https://medium.freecodecamp.com/node-js-child-processes-everything-you-need-to-know-e69498fe970a) | |||
> * 原文作者:本文已获原作者 [Samer Buna](https://medium.freecodecamp.com/@samerbuna) 授权 | |||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | |||
> * 译者: | |||
> * 译者:[熊贤仁](https://github.com/FrankXiong) | |||
> * 校对者: | |||
|
|||
--- | |||
|
|||
# Node.js Child Processes: Everything you need to know |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注意删除原文
Single-threaded, non-blocking performance in Node.js works great for a single process. But eventually, one process in one CPU is not going to be enough to handle the increasing workload of your application. | ||
|
||
Node.js 的单线程、非阻塞执行特性在单进程下工作的很好。但是,单 CPU 中的单进程最终不足以处理应用中增长的工作负荷。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
�这个符号是为何?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我这里看没有这个符号啊
> This article is a write-up of part of [my Pluralsight course about Node.js](https://www.pluralsight.com/courses/nodejs-advanced). I cover similar content in video format there. | ||
|
||
> 这篇文章是[我的 Node.js 视频教学课程](https://www.pluralsight.com/courses/nodejs-advanced)的补充。在课程中以视频的形式描述了和这篇文章相似的内容。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[在课程中以视频的形式描述了和这篇文章相似的�内容。] => [在课程的视频中也讲到了相似的内容。]
The `child_process` module enables us to access Operating System functionalities by running any system command inside a, well, child process. | ||
|
||
`child_process` 模块通过在子进程内部运行一些系统指令,赋予我们使用操作系统功能的能力。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[child_process
模块通过在子进程内部运行一些系统指令,赋予我们使用操作系统功能的能力。] =>[child_process
模块通过在一个子进程中执行系统命令,使得我们能够使用操作系统。]
*Note that examples I’ll be using in this article are all Linux-based. On Windows, you need to switch the commands I use with their Windows alternatives.* | ||
|
||
*注意:这篇文章举的所有例子都基于 Linux。如果在 Windows 上,你要切换为它们对应的 Window 命令。* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
斜体中文需改为加粗(详见wiki排版)
@@ -200,34 +301,60 @@ We can execute it in the background using the `detached` option: | |||
|
|||
The exact behavior of detached child processes depends on the OS. On Windows, the detached child process will have its own console window while on Linux the detached child process will be made the leader of a new process group and session. | |||
|
|||
分离的子进程的具体行为取决于操作系统。Windows 上,分离的子进程有自己的控制台窗口,然而在 Linux 上,分离的子进程会成为新的进程组和会话的领导者。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[领导者。] => [领导进程。]
If you need to execute a file without using a shell, the `execFile` function is what you need. It behaves exactly like the `exec` function but does not use a shell, which makes it a bit more efficient. On Windows, some files cannot be executed on their own, like `.bat` or `.cmd` files. Those files cannot be executed with `execFile` and either `exec` or `spawn` with shell set to true is required to execute them. | ||
|
||
如果你需要执行一个文件时不使用 shell,`execFile` 函数正是你想要的。它的行为跟 `exec` 函数一模一样,但没有使用 shell,这会让它更有效率。Windows 上,一些文件不能在它们自己之上执行,比如 `.bat` 或者 `.cmd` 文件。这些文件不能使用 `execFile` 执行,并且执行它们时,需要将 shell 设置为 true,且只能使用 `exec`、`spawn` 两者之一。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[如果你需要执行一个文件时不使用 shell,execFile
函数正是你想要的。] => [如果你不想用 shell 执行一个文件,那么 execFile
函数正是你想要的。]
#### The *Sync function | ||
|
||
#### *Sync 函数 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注意调整本段所有斜体字
That’s all I have for this topic. Thanks for reading! Until next time! | ||
|
||
以上就是我针对这个话题要讲的全部。感谢阅读!再次再见! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[再次再见!] =>[再见!]
@@ -1,81 +1,134 @@ | |||
> * 原文地址:[Node.js Child Processes: Everything you need to know](https://medium.freecodecamp.com/node-js-child-processes-everything-you-need-to-know-e69498fe970a) | |||
> * 原文作者:本文已获原作者 [Samer Buna](https://medium.freecodecamp.com/@samerbuna) 授权 | |||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | |||
> * 译者: | |||
> * 译者:[熊贤仁](https://github.com/FrankXiong) | |||
> * 校对者: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -179,16 +272,24 @@ Another option we can use is the `env` option to specify the environment variabl | |||
|
|||
The echo command above does not have access to the parent process’s environment variables. It can’t, for example, access `$HOME`, but it can access `$ANSWER` because it was passed as a custom environment variable through the `env` option. | |||
|
|||
上面的 echo 命令没有访问父进程环境变量的权限。比如,它不能访问 `$HOME` 目录,但它可以访问 `$ANSWER` 目录,因为通过 `env` 选项,它被传递了一个指定的环境变量。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[因为通过 env
选项,它被传递了一个指定的环境变量。] -> [因为它通过env
选项接受到了一个指定的环境变量。]
@@ -1,81 +1,134 @@ | |||
> * 原文地址:[Node.js Child Processes: Everything you need to know](https://medium.freecodecamp.com/node-js-child-processes-everything-you-need-to-know-e69498fe970a) | |||
> * 原文作者:本文已获原作者 [Samer Buna](https://medium.freecodecamp.com/@samerbuna) 授权 | |||
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) | |||
> * 译者: | |||
> * 译者:[熊贤仁](https://github.com/FrankXiong) | |||
> * 校对者: | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FrankXiong 校对完毕
@FrankXiong 两位校对者都已经校对好了~ 可以来根据校对意见进行调整了哈 ┏ (゜ω゜)=☞ |
感谢 @CACppuccino @wilsonandusa 的校对,已根据校对意见完成修改 @sqrthree 。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有一些小问题麻烦调整下哈
|
||
No matter how powerful your server may be, a single thread can only support a limited load. | ||
Node.js 的单线程、非阻塞执行特性在单进程下工作的很好。但是,单 CPU 中的单进程最终不足以处理应用中增长的工作负荷。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我这里看没有这些奇怪的符号,系统 macOS 10.12.3,编辑器 VSCode v1.13.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub 上有哎,你在你的仓库看下试试。
|
||
#### The *Sync function | ||
#### *Sync 函数 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个星号删除吧,应该是转换时的遗留问题。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*应该是个通配符吧,代指所有同步函数,比如spawnSync()、forkSync()等。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
了然。
|
||
We first move the whole `longComputation` function into its own file and make it invoke that function when instructed via a message from the main process: | ||
我们首先移动整个 `longComputation` 函数到它自己的文件,并在主进程通过消息发出通知时,在文件中调用这个函数: | ||
|
||
In a new `compute.js` file: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这句没有翻译
格式调整好了 @sqrthree |
@FrankXiong 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。 |
初翻完成