I am running a python script through a node child_process. This python script downloads a file to disk then returns the filename to node. The problem I'm having is that the file saved by python doesn't appear in my files directory until after the node process closes (by throwing an error since it is trying to read this file saved by python). I am not sure how to get python to "instantly" save the file so my node process can then read it.
Node code:
var sent = false;
const CP = require("child_process");
var process = CP.spawn('python', ["../pyScripts/download.py", vid_url]);
process.stdout.on('data', function(data){
console.log("recieved data")
if(sent === false){
sent = true
process.kill('SIGINT');
doStuffWithFile(`${data.toString()}.mp4`);
}
});
Python code:
#*code that grabs file bytes*
with open(f'{filename}.mp4', "wb") as out:
out.write(vid_bytes)
print(filename)
When you write to a file, don't forget to close it. Try this:
#*code that grabs file bytes*
with open(f'{filename}.mp4', "wb") as out:
out.write(vid_bytes)
out.close()
print(filename)
Wait for exit event, which indicates the Python process is finished.
node.js:
var filename = '';
var sent = false;
var fs = require("fs");
const cp = require("child_process");
var process = cp.spawn('python3', ["/tmp/foo.py", "https://dl8.webmfiles.org/elephants-dream.webm"]);
process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
filename = data.toString().trim(); // remove trailing new line!
});
process.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
console.log('Reading: ' + filename)
var stats = fs.statSync(filename)
var fileSizeInBytes = stats.size;
var fileSizeInMegabytes = fileSizeInBytes / (1024*1024);
console.log(`Python downloaded ${fileSizeInMegabytes} MB`)
});
Python:
import time
import requests
import sys
import shutil
filename = '/tmp/test.webm'
headers = {'User-Agent': 'Mozilla 5.0'}
response = requests.get(sys.argv[1], stream=True, headers=headers)
with open(filename, 'wb') as out_file: # with open(...) closes the file automatically
shutil.copyfileobj(response.raw, out_file)
print(filename)
Out:
stdout: /tmp/test.webm
child process exited with code 0
Reading: /tmp/test.webm
Python downloaded 8.169429779052734 MB