spawn

Fork a child process and inject dependency

给你一囗甜甜゛ 提交于 2019-11-30 09:09:55
I currently have an operation in a module that is blocking, so I'm looking at making this into a child process that I fork instead. If I want to do that, then I of course need to modify the architecture of my module. The module requires that a dependency is injected by calling the module as a function, passing in the dependency, like so: var dependency = { name: "Bob" } require('worker')(dependency) Then in my worker module: module.exports = function (dependency) { // Outputs { name: "Bob" } console.log(dependency) } How can I turn this example into a child process being forked? When using

Why is my Node child process that I created via spawn() hanging?

一个人想着一个人 提交于 2019-11-30 04:20:11
问题 I am using spawn() to make a git call. Sometimes it works fine but others it appears to be hanging. I see no events firing (error, exit, close) yet I see evidence that the process did in fact complete successfully. var spawn = require('child_process').spawn; spawn('git', ['push', 'origin', 'master']) .on('error', function(error) { console.log("ERROR: DETAILS: " + error); }) .on('close', function(code) { console.log("SUCCESS: CODE: " + code); }) .on('exit', function(code) { console.log("EXIT:

Grunt spawned process not capturing output

房东的猫 提交于 2019-11-30 01:08:38
问题 I have spawned a process using Grunt, but nothing that is written to the output stream (such as console.log ) is being displayed in the console. I would like Grunt to display any output from the process. grunt.util.spawn( { cmd: 'node' , args: ['app.js'] , opts: { stdio: [ process.stdin , process.stout , process.stderr ] } }) 回答1: Try setting it to opts: {stdio: 'inherit'} . Otherwise you can pipe the output: var child = grunt.util.spawn({ cmd: process.argv[0], // <- A better way to find the

nodejs/express - stream stdout instantly to the client

时光怂恿深爱的人放手 提交于 2019-11-30 00:22:08
I spawned the following child: var spw = spawn('ping', ['-n','10', '127.0.0.1']) and I would like to receive the ping results on the client side ( browser ) one by one , not as a whole. So far I tried this: app.get('/path', function(req, res) { ... spw.stdout.on('data', function (data) { var str = data.toString(); res.write(str + "\n"); }); ... } and that: ... spw.stdout.pipe(res); ... In both cases browser waits 10 of the pings to complete, and then prints the result as a whole. I would like to have them one by one, how to accomplish that? (Client is just making a call to .../path and console

Running Node app through Grunt

心不动则不痛 提交于 2019-11-29 03:42:54
I am trying to run my Node application as a Grunt task. I need to spawn this as a child process, however, to allow me to run the watch task in parallel. This works: grunt.registerTask('start', function () { grunt.util.spawn( { cmd: 'node' , args: ['app.js'] }) grunt.task.run('watch:app') }) However, when changes are detected by the watch task, this will trigger the start task again. Before I spawn another child process of my Node app, I need to kill the previous one. I can't figure out how to kill the process, however. Something like this does not work: var child grunt.registerTask('start',

Node.js spawning a child process interactively with separate stdout and stderr streams

不羁岁月 提交于 2019-11-28 21:30:49
Consider the following C program (test.c): #include <stdio.h> int main() { printf("string out 1\n"); fprintf(stderr, "string err 1\n"); getchar(); printf("string out 2\n"); fprintf(stderr, "string err 2\n"); fclose(stdout); } Which should print a line to stdout, a line to stderr, then wait for user input, then another line to stdout and another line to stderr. Very basic! When compiled and run on the command line the output of the program when complete (user input is received for getchar()): $ ./test string out 1 string err 1 string out 2 string err 2 When trying to spawn this program as a

Node.js spawn child process and get terminal output live

时光毁灭记忆、已成空白 提交于 2019-11-28 13:44:54
问题 I have a script that outputs 'hi', sleeps for a second, outputs 'hi', sleeps for 1 second, and so on and so forth. Now I thought I would be able to tackle this problem with this model. var spawn = require('child_process').spawn, temp = spawn('PATH TO SCRIPT WITH THE ABOVE BEHAVIOUR'); temp.stdout.pipe(process.stdout); Now the problem is that the task needs to be finished in order for the output to be displayed. As I am understanding it, this is due to the fact that the newly spawned process

Wildcards in child_process spawn()?

会有一股神秘感。 提交于 2019-11-28 13:19:52
I want to execute a command like "doSomething ./myfiles/*.csv" with spawn in node.js. I want to use spawn instead of exec, because it is some kind of watch process and I need the stdout output. I tried this var spawn = require('child_process').spawn; spawn("doSomething", ["./myfiles/*.csv"]); But then the wildcard *.csv will not interpreted. Is it not possible to use wildcards when using spawn()? Are there other possibilities to solve this problem? Thanks Torben The * is being expanded by the shell, and for child_process.spawn the arguments are coming through as strings so will never get

How to spawn a new independent process in Python

£可爱£侵袭症+ 提交于 2019-11-27 22:01:36
I have a some Python code that occasionally needs to span a new process to run a shell script in a "fire and forget" manner, i.e. without blocking. The shell script will not communicate with the original Python code and will in fact probably terminate the calling Python process, so the launched shell script cannot be a child process of the calling Python process. I need it to be launched as an independent process. In other words, let's say I have mycode.py and that launches script.sh. Then mycode.py will continue processing without blocking. The script script.sh will do some things

Running Node app through Grunt

ε祈祈猫儿з 提交于 2019-11-27 17:46:07
问题 I am trying to run my Node application as a Grunt task. I need to spawn this as a child process, however, to allow me to run the watch task in parallel. This works: grunt.registerTask('start', function () { grunt.util.spawn( { cmd: 'node' , args: ['app.js'] }) grunt.task.run('watch:app') }) However, when changes are detected by the watch task, this will trigger the start task again. Before I spawn another child process of my Node app, I need to kill the previous one. I can't figure out how to