यह अब बहुत आसान है (6 साल बाद)!
स्पॉन एक चाइल्ड ऑबजेक्ट लौटाता है , जिसके साथ आप घटनाओं को सुन सकते हैं । घटनाएँ हैं:
- क्लास: चाइल्डप्रोसेस
- घटना: 'त्रुटि'
- घटना: 'बाहर निकलें'
- घटना: 'करीब'
- घटना: 'डिस्कनेक्ट'
- घटना: 'संदेश'
चाइल्डऑबजेक्ट से वस्तुओं का एक गुच्छा भी है , वे हैं:
- क्लास: चाइल्डप्रोसेस
- child.stdin
- child.stdout
- child.stderr
- child.stdio
- child.pid
- child.connected
- child.kill ([संकेत])
- चाइल्ड.सेंड (संदेश [, sendHandle] [, कॉलबैक])
- child.disconnect ()
चाइल्डऑबजेक्ट के बारे में अधिक जानकारी यहाँ देखें: https://nodejs.org/api/child_process.html
अतुल्यकालिक
यदि आप अपनी प्रक्रिया को पृष्ठभूमि में चलाना चाहते हैं जबकि नोड अभी भी निष्पादित करना जारी रखने में सक्षम है, तो एसिंक्रोनस विधि का उपयोग करें। आप अपनी प्रक्रिया पूरी होने के बाद भी कार्रवाई करना चुन सकते हैं, और जब प्रक्रिया में कोई आउटपुट होता है (उदाहरण के लिए यदि आप क्लाइंट को स्क्रिप्ट का आउटपुट भेजना चाहते हैं)।
child_process.spawn (...); (नोड v0.1.90)
var spawn = require('child_process').spawn;
var child = spawn('node ./commands/server.js');
// You can also use a variable to save the output
// for when the script closes later
var scriptOutput = "";
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) {
//Here is where the output goes
console.log('stdout: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
//Here is where the error output goes
console.log('stderr: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.on('close', function(code) {
//Here you can get the exit code of the script
console.log('closing code: ' + code);
console.log('Full output of script: ',scriptOutput);
});
यहां बताया गया है कि आप कॉलबैक + अतुल्यकालिक विधि का उपयोग कैसे करेंगे :
var child_process = require('child_process');
console.log("Node Version: ", process.version);
run_script("ls", ["-l", "/home"], function(output, exit_code) {
console.log("Process Finished.");
console.log('closing code: ' + exit_code);
console.log('Full output of script: ',output);
});
console.log ("Continuing to do node things while the process runs at the same time...");
// This function will output the lines from the script
// AS is runs, AND will return the full combined output
// as well as exit code when it's done (using the callback).
function run_script(command, args, callback) {
console.log("Starting Process.");
var child = child_process.spawn(command, args);
var scriptOutput = "";
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.on('close', function(code) {
callback(scriptOutput,code);
});
}
उपरोक्त विधि का उपयोग करना, आप उत्पादन के प्रत्येक पंक्ति की स्क्रिप्ट से ग्राहक के लिए (उदाहरण के लिए, जब आप पर घटनाओं प्राप्त प्रत्येक पंक्ति भेजने के लिए Socket.io का उपयोग कर भेज सकते हैं stdout
या stderr
)।
एक समय का
यदि आप चाहते हैं कि नोड यह क्या कर रहा है और स्क्रिप्ट के पूरा होने तक प्रतीक्षा करें , तो आप तुल्यकालिक संस्करण का उपयोग कर सकते हैं:
child_process.spawnSync (...); (नोड v0.11.12 +)
इस पद्धति के मुद्दे:
- यदि स्क्रिप्ट को पूरा होने में थोड़ा समय लगता है, तो आपका सर्वर उस समय के लिए लटका रहेगा!
- स्क्रिप्ट समाप्त होने के बाद ही स्टडआउट लौटाया जाएगा । क्योंकि यह समकालिक है, यह तब तक जारी नहीं रह सकता जब तक कि वर्तमान रेखा समाप्त नहीं हो जाती। इसलिए स्पॉन लाइन समाप्त होने तक यह 'stdout' इवेंट को कैप्चर करने में असमर्थ है।
इसे कैसे उपयोग करे:
var child_process = require('child_process');
var child = child_process.spawnSync("ls", ["-l", "/home"], { encoding : 'utf8' });
console.log("Process finished.");
if(child.error) {
console.log("ERROR: ",child.error);
}
console.log("stdout: ",child.stdout);
console.log("stderr: ",child.stderr);
console.log("exist code: ",child.status);
python
तो-u
कंसोल कंसोल आउटपुट को बफर न करने के लिए इसके लिए झंडा पास करना न भूलें , अन्यथा ऐसा लगेगा कि स्क्रिप्ट लाइव नहीं है stackoverflow.com/a/49947671/906265