I have a nodejs server that calls python code when the user presses a button in html. The python writes to a text file which is then used back in the node. The python runs correctly when ran on its own, but when I run it as a child process it exits instantly and the text files have nothing written to them. The code inside the
This is the function, It still runs as it outputs " is not in the database", but other than that nothing.
function getLinkList(youtubeLink) {
var spawn = require("child_process").spawn;
var process = spawn('python', ['./trancriptdownloader.py', youtubeLink]);
process.on('exit', function(data) {
var wordList = syncReadFile('./getasl/transcript.txt');
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
//checks if the word is in the database
for (let i = 0; i < wordList.length-1; i++) {
var sql = 'SELECT filenames FROM signs WHERE word = ?';
//queries the database to see if word is there
con.query(sql, [wordList[i]], (err, result) => {
if (err) throw err;
else {
result=JSON.parse(JSON.stringify(result));
result = filterContents('{"filenames":', JSON.stringify(result), '');
result = filterContents(/[!"#'$%&()*+,-./:;<=>?@[\]`^_{|}~]/g, result, '');
linkList.push(result);
if(linkList[i] == '') {
console.log(wordList[i] + " is not in the database");
linkList[i] = wordList[i];
}
else {console.log(linkList[i]);}
}
});
//end of query
}
//end of loop
con.end();
});
//end of python
return linkList;
}
//end of function
and here is the python code.
import youtube_transcript_downloader
import contractions
import re
import num2words
import csv
import sys
url = sys.argv[1]
transcript = youtube_transcript_downloader.get_transcript(url)
expanded_words = []
filters = {"\$": "dollar ", "\-": '',"\%": " percent", "\"": "", "\'s": "", "\'":"", "\&": "and", "\:": "", "\;": "", "\<":"", "\=":"", "\>":"", "\?":"", "\@": "at", "\/":" slash ", "\!":"", "\[":"", "\]":"", "\{":"", "\}":"", "\#":"", "\\\\":"", "\,":"", "\.":"", "\(":"", "\)":"", "\*": "", "\~":"", "\`":"", "\_":" ", "\|":"", "\^":"", "\+":""}
#this filters out unwanted stuff + makes numbers actually work
#previously in Java script but changed to prevent � in text file.
#Try to simplify this mess later
def filter(filter, text):
for key, val in filter.items():
text = re.sub(key, val, text)
return text
for key, val in transcript.items():
val = re.sub(r"(\d+)", lambda x: num2words.num2words(int(x.group(0))), val)
val = contractions.fix(val)
val = filter(filters, val)
expanded_words.append(val)
transcript.update({key: val})
with open("./getasl/transcript.txt", "w") as f:
f.write("")
for val in expanded_words:
with open("./getasl/transcript.txt", "a") as f:
f.write(val + "\n")
with open('./getasl/transcript.csv','w') as f:
w = csv.writer(f)
w.writerows(transcript.items())
with open("./getasl/transcript.csv", "r") as f:
print(f.read())
sys.stdout.flush()
I had thought I had fixed this problem a few days ago, but that was because I was testing it with the example video so the text matched from when I was testing it individually.
I double checked that everything runs independently and they do. I moved the child process to my server.js from my index.js to see if it would run correctly there and got the same results.
I changed its parameters to 'close' instead of 'exit' as well as tried 'data'. None of this seemed to help, the python closed within a fraction of a second and then the js reads the black text file.
I've never had to run python in node before so I don't have the experience to really know what to check.