Stream stdout from child process to browser via expressjs

后端 未结 2 671
一个人的身影
一个人的身影 2021-02-03 11:35

We have an app built using nodejs, express and child_process.spawn. One requirement is that we need to spawn a process at runtime and capt

2条回答
  •  孤独总比滥情好
    2021-02-03 12:25

    Another approach would be to use Socket.IO. Socket.IO is designed for this kind of real-time event-based communication between the client and the server.

    The key is to flush the standard output stream after writing to it. This should trigger the "childprocess.stdout.on('data', ...)" event.

    How this is done depends on what language your child process is written in. Say you're running a python script and writing to the standard output using "print", then you'd need to do this:

    import sys
    
    # Flush after each print to stdout
    sys.stdout.flush()
    

    Here's a simple example that implements what you're asking using Socket.IO. Server side code:

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    var child = require('child_process');
    
    app.get('/', function(req, res){
      res.sendFile(__dirname + '/index.html');
    });
    
    http.listen(8000, function(){
    	console.log('listening on *:8000');
    });
    
    io.on('connection', function(socket){
    	console.log('new user connected');
    	
    	var python = child.spawn( 'python', ['compute.py'],[]);
    
    	var chunk = '';
    	python.stdout.on('data', function(data){
    		chunk += data
    		socket.emit('newdata', chunk);
    	} );
    	
    	python.stderr.on('data', function (data) {
    		console.log('Failed to start child process.');
    	})
    })

    On the client, you could then register an event listner:

    
    

提交回复
热议问题