After following @Unitech's answer and running into issues, I discovered that there's a bit more to what's happening. I'm unsure if this has to do with my Node version, or something else. (I'm running v16.13.1 and python 3.10.8) However it appears to be that the way messages are traded has changed. The message now starts with 8 bytes of something, and 8 more bytes which is the message length in little Endian format.
Anyway, I've written the following python code which I've had success with sending and receiving messages with.
import os
import json
# get the FD from ENV
NODEIPCFD = int(os.environ["NODE_CHANNEL_FD"])
def sendMessage(text):
'sends a Node IPC message to parent proccess'
# encode message as json string + newline in bytes
bytesMessage = (json.dumps(text) + "\n").encode()
# I'm not actually sure what this number is for,
# but not including it causes problems.
# probably encodes something to do with the 'advanced' serialization
os.write(NODEIPCFD, int.to_bytes(1, 8, "little"))
# send the length as an 8-byte number in little Endian format
os.write(NODEIPCFD, len(bytesMessage).to_bytes(8, "little"))
# send message
os.write(NODEIPCFD, bytesMessage)
def readMessage():
'read in next message from parent Node process via built-in node IPC'
# read and discard 8 bytes. Again, don't know why...
os.read(NODEIPCFD, 8)
# read and parse 8 bytes as length in little Endian format
length = int.from_bytes(os.read(NODEIPCFD, 8), "little")
# read 'length' bytes and pass to json parser
return json.loads(os.read(NODEIPCFD, length))
I started the python code using this on the node side:
const child_process = require('node:child_process');
var cp = child_process.spawn('python', ['child_process.py'], {
stdio:[null, null, null, 'ipc']
});
I just wish the Node documentation included the underlying process for how messages are traded.