简体   繁体   中英

Child process output to socket.io

Forgive the silly question if so, I'm relatively new to Node.

I'm spawning a child process on my node server to import a dataset to a database. The child process, executing osm2pgsl with parameters, has its own internal output that displays the currently processed data and a count of what's been processed.

I have a simple node script to spawn this process, and log information from this process as and when it arrives. The main info that I need access to isn't polled through stdout, stderr or on , which is problematic.

Node script

var util  = require('util'),
    spawn = require('child_process').spawn,
    file = process.argv[2],
    ls    = spawn('osm2pgsql', ['--slim', '-d', 'gis', '-U', 'postgres', '--number-processes', '3', file]);

ls.stdout.on('data', function (data) {
  process.stdout.write('Currently processing: ' + data.toString() + '\r');
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data.toString());
});

ls.on('exit', function (code) {
  console.log('child process exited with code ' + code.toString());
});

Output

Mid: pgsql, scale=100 cache=800
Setting up table: planet_osm_nodes

stderr: NOTICE:  table "planet_osm_nodes" does not exist, skipping

stderr: Setting up table: planet_osm_ways

stderr: NOTICE:  table "planet_osm_ways" does not exist, skipping

stderr: Setting up table: planet_osm_rels

stderr: NOTICE:  table "planet_osm_rels" does not exist, skipping

stderr: 
Reading in file: /OSMDATA/great-britain-latest.osm.pbf

Processing: Node(10k 10.0k/s) Way(0k 0.00k/s) Relation(0 0.00/s)
Processing: Node(20k 20.0k/s) Way(0k 0.00k/s) Relation(0 0.00/s)
Processing: Node(30k 30.0k/s) Way(0k 0.00k/s) Relation(0 0.00/s)

From the stderr: line, you can see that I'm able to access that stream, but the Processing: ... is what I need access to above all else. This is being printed from within the child process and I'm not sure how to access it directly.

Is there any way of accessing the output (above) from within my Nodejs server?

EDIT : I'm intending on piping this output to Socket.io but I need access to it first, hence the title.

Figured it out but it may be useful to people in the future.

Because Processing: ... was being recursively updated on a single line using \\r in the osm2pgsql source code, it was actually coming out of the stderr in the same manner as everything else.

The output for the Processing: ... is actually the following line:

stderr: 
Reading in file: /OSMDATA/great-britain-latest.osm.pbf

Processing: Node(10k 10.0k/s) Way(0k 0.00k/s) Relation(0 0.00/s)

It didn't occur to me that the output may be multiple lines long.

I'm able to access the output via ls.stderr.on('data', function(data) {} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM