简体   繁体   English

在Python中,如何与正在运行的Node.js进程持续通信?

[英]In Python, how do I continuously communicate with running Node.js process?

(Apologies if this is somewhat vague as I'm just getting started with Python.) (很抱歉,因为我刚开始使用Python,这有点含糊。)

I'm in the process of creating a SublimeText 2 intellisense plugin that communicates with a running command-line node server spawned from a Node.js script. 我正在创建一个SublimeText 2 intellisense插件,该插件与从Node.js脚本生成的正在运行的命令行节点服务器进行通信。 The node application I would like to commnicate with can be found here: https://github.com/clausreinke/typescript-tools . 我想与之通信的节点应用程序可以在这里找到: https : //github.com/clausreinke/typescript-tools (Please note that once the tss command has been called, it starts it's own subprocess command-line repl which needs to receive commands to execute. Getting commands to that command line is an entirely different question in itself, which I have yet to resolve.) (请注意,一旦调用tss命令,它将启动它自己的子进程命令行repl,该repl需要接收要执行的命令。将命令传递到该命令行本身是一个完全不同的问题,我尚未解决。 )

I can successfully "talk" to node via: 我可以通过以下方式成功与节点“对话”:

self.process = subprocess.Popen( self.args,
                stdin = subprocess.PIPE,
                stdout = subprocess.PIPE,
                stderr = subprocess.STDOUT)

self.result = self.process.communicate()[0]

which is invoked using 使用调用

self.thread = NodeJS( args )
self.thread.start()
self.handle_thread( self.thread )

The issue is that once the node.js file is executed, it closes the pipe and exits the IO process. 问题在于,一旦执行了node.js文件,它将关闭管道并退出IO进程。 Subsequent calls to the process returns a 随后对该过程的调用返回一个

ValueError: I/O operation on closed file

My question is: How do I start up the node app, run it in the background, and then "pipe" commands continuously to it from Python? 我的问题是:如何启动节点应用程序,在后台运行它,然后从Python连续“传递”命令给它?

I would like to 我想要

  1. Start the node.js server when the plugin loads and keep it running in the background 插件加载后启动node.js服务器,并使其在后台运行
  2. Listen for key input which i then send to node (already working) 监听键输入,然后将其发送到节点(已经工作)
  3. Pipe the output from the running node process (after it's looked up the definition) back to python and then display the output, but NOT close the thread or the process 将正在运行的节点进程的输出(在查找定义之后)通过管道传递回python,然后显示输出,但不关闭线程或进程

All of the steps are complete except for the continuous asynchronous back-and forth, which I can't seem to figure out. 除了连续异步来回,所有这些步骤都已完成,我似乎无法弄清楚。

Any help would be appreciated. 任何帮助,将不胜感激。

在Python中创建一个Unix域套接字或邮槽,在Node.js中连接到它,并将其用作您的通信通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM