1
我从Python线程中产生了一个Node进程,并通过stdio在它们之间传递数据。在Python将某些内容发送给Node后,Node会启动一个子进程,然后将该子进程的输出发送回Python。通过stdio在Node.js和Python之间进行通信的问题
这个工作,几秒钟,然后没有更多的数据来。但是,如果我杀了Node进程,那么突然所有的数据都会立刻出现。
我认为这是与缓冲有关的事情,但我尝试了很多东西,无法正常工作。
值得一提的是,如果我在Python之外运行监视器,它工作正常,所以这可能是Python方面的一些问题。
相关Python代码:
class MonitorThread(Thread):
# *snip*
def run(self):
self.process = Popen(['node',
path.join(PACKAGE_PATH 'monitor.js')],
stdout=PIPE, stdin=PIPE, stderr=PIPE)
while self.process.poll() is None:
stdout = self.process.stdout.readline().rstrip('\n')
if stdout:
main_thread(debug, stdout)
stderr = self.process.stderr.readline().rstrip('\n')
if stderr:
main_thread(debug, stderr)
#time.sleep(0.1)
相关的Node.js代码(在CoffeeScript中,但即使你不知道你的想法):
# *snip*
child = spawn cmd, options
child.stdout.on 'data', (data) ->
process.stdout.write "stdout: #{data.toString().trim()}\n"
child.stderr.on 'data', (data) ->
process.stdout.write "stderr: #{data.toString().trim()}\n"
还有很多其他的代码,但它并不真正相关,数据正在发送,然后数据正在被接收,只是一会儿。当我手动杀死它时,它仍然在运行,其余数据突然出现。
Monitor [send] - {"wrap": false, "directories": {"src": "lib"}, "id": 0, "base_dir": "C:\\Users\\Joe\\Documents\\GitHub\\CoffeeScript-Sublime-Plugin"}
Monitor [recv] - 13:55:30 - compiled src\a.coffee
Monitor [recv] - path.exists is now called `fs.exists`.
Monitor [recv] - 13:55:30 - compiled src\b.coffee
- 我试过util.pump()
- 我试过,与重生()调用,标准输入输出: '继承'
- 我试着等待“漏'发送更多数据之前的事件。
看来,输出缓冲。这可能有助于防止缓冲:http://stackoverflow.com/questions/6471004/how-can-i-write-blocking-in-stdout-with-node-js – 2012-07-26 13:14:36
这似乎没有什么差别,以及实际上它加剧了这个问题。 只有一条线通过而不是三条,当然现在没有什么东西可以通过(因为没有缓冲)而被杀死。 – Xavura 2012-07-26 13:21:29
似乎我仍然有stdio:'继承'。删除后,我得到:EBADF - 错误的文件描述符。编辑:如果运行立体,我明白了。如果从Python内部运行,则不会出错,但不会输出。 – Xavura 2012-07-26 13:28:17