如何派生一个侦听与父进程不同的调试端口的子进程 [英] How to fork a child process that listens on a different debug port than the parent
问题描述
我正在尝试使用child_process.fork
生成一个中断并侦听V8调试协议的进程.
I am trying to use child_process.fork
to spawn a process that breaks and listens on the V8 debug protocol.
但是,我无法让派生进程监听与父进程不同的端口.假设父进程在6000
上侦听,子进程也尝试在端口6000
上侦听:
However, I can't get the forked process to listen on a port that's different from the parent process. Assuming the parent process listens on 6000
, the child process also attempts to listen on port 6000
:
无法打开端口6000上的套接字,请等待1000毫秒,然后重试
Failed to open socket on port 6000, waiting 1000 ms before retrying
这是我的代码:
// `test.js`, invoked as `node --debug-brk=6000 test.js`
var nodeModule, args, env, child
nodeModule = path.normalize(path.join(__dirname, '..', 'app.js'))
args = [
'--debug-brk=6001'
, '127.0.0.1'
, 3030
, 'api-testing'
]
env = { 'DB_URI': 'mongodb://localhost/test' }
child = require('child_process')
.fork(nodeModule, args, {env: env})
.on('message', callback)
如您所见,我正在尝试让分叉的进程在端口6001
上进行侦听,但是子进程尝试在父级正在使用的端口6000
上进行侦听.
As you can see, I'm trying to get the forked process to listen on port 6001
, but the child process attempts to listen on port 6000
which is in use by the parent.
如何让子进程在端口6001
或其他一些空闲端口上侦听?
How can I get the child process to listen on port 6001
, or some other free port?
关于此主题有多个主题.例如:
There are several threads on this subject. For example:
- How to debug Node.JS child forked process?
- Debugging Node.js processes with cluster.fork()
但是:
- 这些线程处理叉的
cluster
变体 - 请参阅
execArgv
,该文件似乎在中没有针对process
的记录,并且仍未记录在cluster
中.
- These threads deal with the
cluster
variant of forking - Refer to
execArgv
, which appear to have been undocumented forprocess
and are still undocumented forcluster
.
推荐答案
Simple enough answer, found on this comment and with some help from #Node.js on Freenode:
只需将--debug-brk
移至options
参数的execArgv
键中,然后将其移至fork
:
Just move the --debug-brk
into the execArgv
key of the options
param to fork
:
// Excerpt:
args = [
'127.0.0.1'
, 3030
, 'api-testing'
]
env = { 'DB_URI': 'mongodb://localhost/test' }
child = fork(nodeModule, args, {
env: env
, execArgv: ['--debug-brk=6001']
})
.on('message', this.callback)
execArgv
是传递给节点进程的参数数组. argv
是传递到主模块的集合.对于argv
,child_process.fork
有一个专用参数,但是execArgv
必须放置在opts
参数中.这行得通,在子进程中,我们有:
execArgv
is the array of parameters passed to the node process. argv
is the set passed to the main module. There's a dedicated parameter to child_process.fork
for argv
, but execArgv
s have to be placed within the opts
param. This works, and in the child process we have:
> process.execArgv
["--debug-brk=6001"]
> process.argv
["/usr/local/Cellar/node/0.10.13/bin/node" "/Users/dmitry/dev/linksmotif/app.js", "127.0.0.1", "3030", "api-testing"]
总结
Node.js始终将execArgv
和argv
视为单独的值集.
Node.js consistently treats execArgv
and argv
as separate sets of values.
这篇关于如何派生一个侦听与父进程不同的调试端口的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!