简体   繁体   English

子进程输出到socket.io

[英]Child process output to socket.io

Forgive the silly question if so, I'm relatively new to Node. 如果可以,请原谅这个愚蠢的问题,我对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. 使用参数执行osm2pgsl的子进程具有其自己的内部输出,该输出显示当前已处理的数据以及已处理内容的计数。

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. 我需要访问的主要信息不是通过stdout, stderr or on轮询的,这是有问题的。

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. stderr:行中,您可以看到我可以访问该流,但是我首先需要访问Processing: ... 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? 有什么方法可以从我的Nodejs服务器中访问输出(上方)?

EDIT : I'm intending on piping this output to Socket.io but I need access to it first, hence the title. 编辑 :我打算将此输出传递到Socket.io,但我需要首先访问它,因此需要标题。

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. 因为正在使用osm2pgsql源代码中的\\r在一行上递归更新Processing: ... ,所以实际上它以与其他所有方式相同的方式从stderrstderr

The output for the Processing: ... is actually the following line: Processing: ...的输出实际上是以下行:

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) {} ); 我可以通过ls.stderr.on('data', function(data) {} );访问输出ls.stderr.on('data', function(data) {} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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