Understanding nodejs

本文深入探讨Node.js作为JavaScript运行时的独特之处,包括其单线程模型如何避免死锁问题,事件循环机制如何提高并发处理能力,以及V8引擎如何确保高性能的JavaScript执行。此外,还介绍了Node.js丰富的模块系统及其在前后端开发中的应用。

本文参考:Felix Geisendörfer Understanding node.js

nodejs是一个基于V8的JavaScript Runtime平台。 nodejs是众多解释型语言解释器的一种,类似于ruby, python, php, java等。与其他解释器相比,nodejs有许多独特的特性。

Keywords

  1. Single Thread
  2. Event Loop/Non-Block
  3. JavaScript
  4. V8
  5. Module

Single Thread

nodejs以单线程的方式的执行程序,因此不会出现多个线程访问同一变量的情况(死锁、锁竞争),不需要复杂、危险的并发编程,这将简化程序的开发,提高开发效率。

Event Loop/Non-Block

nodejs通过事件轮询的方式来实现并发。对于耗时的I/O操作,nodejs保存回调,任务以异步的方式在后台执行,运行环境继续单线程地执行代码,等I/O操作结束,数据可用时,回调方法会被调用。
这种非阻塞的方式避免了时间浪费在耗时的I/O操作上,因引起可以大大提高并发处理能力。
nodejs采用的I/O机制是轻量级的,能最大程度地利用系统的I/O能力。

JavaScript

使用者众多,使用广泛,GitHub上最受欢迎。
大量的JavaScript库。
前端和后端语言统一。
易学,易上手。

V8

Chrome之所以成为最快的浏览器,就是因为它采用了V8作为JavaScript解释器。一直以来,Google在不断地优化V8引擎,它已经成为最快的动态语言解释器之一。

Module

nodejs对JS作了一些优化和扩展。并提供了一些额外的API,如文件系统、网络访问等。
同时,开发者也很容易为nodejs编写新的模块库。

### Node.js Parent Child Process Communication Using Pipe In Node.js, inter-process communication (IPC) channels facilitate message passing between parent and child processes. When establishing an IPC channel through `child_process.fork()`, the `subprocess.send()` method allows sending messages from the parent to the child process[^1]. For receiving these messages within a Node.js environment as the child process, one listens for the 'message' event. However, specifically discussing **communication using pipes**, it's important to note that while IPC provides high-level abstractions over underlying OS mechanisms like named or unnamed pipes on Unix-like systems, direct manipulation of such pipes isn't typically necessary due to the abstraction provided by Node.js APIs. Yet understanding how data flows through streams created during spawning can offer deeper insight into lower-level operations. For scenarios where explicit use of pipes is desired outside of what `fork` offers automatically: - A custom approach involves utilizing the standard input/output streams (`stdin`, `stdout`) available when creating a new process with methods other than fork, such as spawn. Below demonstrates setting up basic pipe-based communication without relying on automatic IPC setup offered by `fork`. ```javascript // parent.js const {spawn} = require('child_process'); let ls = spawn('node', ['child.js']); ls.stdout.on('data', function(data){ console.log(`Received from child: ${data.toString().trim()}`); }); console.log("Sending message to child"); ls.stdin.write(JSON.stringify({msg:"Hello"})); ``` ```javascript // child.js process.stdin.setEncoding('utf8'); process.stdin.on('readable', () => { let chunk; while(null !== (chunk = process.stdin.read())){ try{ const parsedMsg=JSON.parse(chunk); console.log(`Message received:${parsedMsg.msg}`); process.stdout.write("Acknowledged\n"); }catch(e){ console.error("Failed parsing:",e.message); } } }); ``` This example shows manual handling of JSON-encoded strings sent across the pipe connecting parent and child nodes. Note that this form of interaction requires careful management of encoding/decoding rules agreed upon both ends.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值