0

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.

  • Ignore where it says filenames instead of links, I converted the code over to use links instead, and have yet to change my database's names – First Name Last Name Nov 04 '22 at 23:45
  • Does this answer your question? [How to wait for a child process to finish in Node.js](https://stackoverflow.com/questions/22337446/how-to-wait-for-a-child-process-to-finish-in-node-js) – Fraser Nov 05 '22 at 00:08
  • 1
    No it doesn’t seem to…if it helps I forgot to mention, the python seems to exit with the value two @Fraser – First Name Last Name Nov 05 '22 at 00:41
  • `2` is the exit code - `exit` passes the exit code and signal - not data. An error code of 2 is usually `file not found`. This to me would suggest that your python script isn't correctly picking up a file that is referenced. Almost certainly a path issue. Try making your paths `"./getasl/transcript.txt"` etc absolute rather than relative - or else ensuring they are relative to the path that they are called from. Pretty sure that is the root of your issue :) – Fraser Nov 05 '22 at 07:47

0 Answers0