【发布时间】:2016-09-04 17:08:35
【问题描述】:
是的,你好。我最近了解到 Node.js 是单线程的。我想更快地使用我的应用程序(MongoDB/Express 应用程序),所以我编写了以下脚本(我希望它使用 8 个处理器)
#!/bin/bash
node app.js &
node app.js &
node app.js &
node app.js &
node app.js &
node app.js &
node app.js &
node app.js &
当我尝试运行脚本时,我收到很多关于正在使用的端口的错误,但我知道 TCP 允许 65536 个端口,它应该只尝试使用 8 个。我是否需要将我的 Node.js 更新为新的版本?
我在 Amazon Linux 上运行。
谢谢。
【问题讨论】:
-
您可能已将其配置为始终使用相同的端口。
-
使用 node.js
cluster模块启动 N 个进程来处理同一端口上的所有请求。注意:如果您的应用程序是使用适当的异步 I/O 编写的,并且您是数据库绑定的(例如,数据库限制了您的响应时间),那么启动更多进程可能无济于事。 Node.js 可能是单线程的,但您的数据库可能是它自己的进程,并且 node.js 可以通过适当的异步 I/O 编码一次处理许多请求。
标签: javascript node.js multithreading mongodb parallel-processing