I have an app.js
file that uses fs.watchFile()
to check for changes in a text file. The changes are then outputed to the console. I have a python.py
that I would like to use this JS file while the same time running code for the GPIO pins on a RasberryPi (the GPIO code is not shown here for simplicity). When running python.py
the program gets stuck after priting "after start" which means that the JS file is at fault. I have tried using Python asyncio
as well but I've received the same result as when I don't use it. Is there a way of allowing an asynchronous process on Node.js to run, while letting be a Python subprocess and allow the rest of the code to continue normally? Should I try running Python using Node.js instead?
app.js:
const fs = require('fs');
function main() {
const text_file= '/home/user/git/corp/file.txt';
const text = fs.readFileSync(policies_file, "utf8");
console.log(text);
fs.watchFile(text_file, {bigint: false, persistent: true, interval: 1}, (curr, prev) => {
const text= fs.readFileSync(text_file, "utf8");
console.log(text);
}
main();
python.py
import subprocess
import asyncio
async def subproc():
print("start subprocess")
proc = subprocess.Popen(["/usr/bin/node","/home/user/git/corp/prudens-js/node/app.js", ], stdout=subprocess.PIPE)
print("after start")
out = proc.communicate()
print("after finish")
print(out)
async def main():
print("start")
await asyncio.gather(subproc())
print("waiting")
asyncio.run(main())