1const path = require('path')
2const {spawn} = require('child_process')
3/**
4 * Run python myscript, pass in `-u` to not buffer console output
5 * @return {ChildProcess}
6*/
7function runScript(){
8 return spawn('python', [
9 "-u",
10 path.join(__dirname, 'myscript.py'),
11 "--foo", "some value for foo",
12 ]);
13}
14const subprocess = runScript()
15// print output of script
16subprocess.stdout.on('data', (data) => {
17 console.log(`data:${data}`);
18});
19subprocess.stderr.on('data', (data) => {
20 console.log(`error:${data}`);
21});
22subprocess.stderr.on('close', () => {
23 console.log("Closed");
24});
1#Import library
2import sys, getopt, time
3def main(argv):
4 argument = ''
5 usage = 'usage: myscript.py -f <sometext>'
6 # parse incoming arguments
7 try:
8 opts, args = getopt.getopt(argv,"hf:",["foo="])
9 except getopt.GetoptError:
10 print(usage)
11 sys.exit(2)
12 for opt, arg in opts:
13 if opt == '-h':
14 print(usage)
15 sys.exit()
16 elif opt in ("-f", "--foo"):
17 argument = arg
18 # print output
19 print("Start : %s" % time.ctime())
20 time.sleep( 2 )
21 print('Foo is')
22 time.sleep( 2 )
23 print(argument)
24 print("End : %s" % time.ctime())
25if __name__ == "__main__":
26main(sys.argv[1:])