简体   繁体   English

如何在 NodeJS 和 Python (pynput) 之间与子进程通信

[英]How to communicate between NodeJS and Python (pynput) with child process

I made a script in python which uses pynput and displays the keys pressed from a keyboard in the console.我在 python 中制作了一个脚本,它使用 pynput 并显示从控制台中的键盘按下的键。

from pynput.keyboard import Listener

def on_press(key):
    try:
        print('keydown : {0}'.format(
            key.char))
    except AttributeError:
        print('keydown : {0}'.format(
            key))

def on_release(key):
    try:
        print('keyup : {0}'.format(
            key.char))
    except AttributeError:
        print('keyup : {0}'.format(
            key))


with Listener(on_press=on_press, on_release=on_release) as listener:  
    listener.join()

I point out that I have never made a python in my life, and that this code works when I run it.我指出,我一生中从未制作过 python,并且此代码在我运行时有效。 We get the expected result:我们得到预期的结果:

keydown : a
keyup : a
keydown : b
keyup : b
keydown : Key.enter
keyup : Key.enter

However, I want to run it in a child process with NodeJS:但是,我想用 NodeJS 在子进程中运行它:

const child = require('child_process')
var py = child.spawn('python3', ['myFile.py'])
py.sdout.on('data', (data) => { console.log(data.toString()) })
py.stderr.on('data', (data) => { console.error(data.toString()) })

But when I execute the javascript file with NodeJS, I do not receive any data or error when I press a key... Yet my child process works with any other python script...但是,当我使用 NodeJS 执行 javascript 文件时,当我按下一个键时,我没有收到任何数据或错误……但是我的子进程可以与任何其他 python 脚本一起使用……

If anyone knows why I can't do it or knows a solution, thank you in advance for their response.如果有人知道我为什么不能这样做或知道解决方案,请提前感谢您的回复。

As I pointed out in the comments, your Node.js app does not pass its input stream to its child process, nor its child process finishes for their parent to read their output.正如我在评论中指出的那样,您的 Node.js 应用程序不会将其输入 stream 传递给其子进程,其子进程也不会完成其父进程以读取其 output。 So here's an example that works, but only reads the first keypress before finishing:所以这是一个有效的例子,但在完成之前只读取第一个按键:

Node.js (same script, but we pipe the keypresses to the child process): Node.js(相同的脚本,但我们 pipe 对子进程的按键):

const { spawn } = require('child_process');
const readline = require('readline');

const py = spawn('python3', ['/home/telmo/Desktop/myFile.py']);

readline.emitKeypressEvents(py.stdin);
process.stdin.setRawMode(true);

py.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

py.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

py.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

Python (same script, but we finish after the first key up event): Python(相同的脚本,但我们在第一个按键事件之后完成):

from pynput.keyboard import Listener
import sys

def on_press(key):
    try:
        print('keydown : {0}'.format(
            key.char))
    except AttributeError:
        print('keydown : {0}'.format(
            key))

def on_release(key):
    try:
        print('keyup : {0}'.format(
            key.char))
        sys.exit()
    except AttributeError:
        print('keyup : {0}'.format(
            key))

with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

Running these when I pressed 'a' I got the following output:当我按下“a”时运行这些,我得到以下 output:

stdout: keyup : Key.enter
keydown : a
keyup : a

child process exited with code 0

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

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