NodeJS:如何正确并行运行spawn?

时间:2018-10-24 20:56:17

标签: node.js spawn

我正在运行一个for循环,在其中执行以下操作:

let perform_vrp = function() {
    //..
    perform_tsp();
}

let perform_tsp = function() {
  //..

  const pyProg = spawn('python3', [process.env.PWD + '/server/vrp_solver/tsp_solver.py', '/../../route_data/' + depot.city + '/' + moment(route.date_driven).format('Y-MM-DD'), 'morning',route.name]);

  winston.info('Solving the TSP for %s...', route.name);

  pyProg.stdout.on('data', function (data) {

    let result_string = data.toString();
    winston.info('Route result for %s is: %s', route.name, result_string);

    let result_array = eval(result_string); 
    //...
  });
}

它基本上为for循环中的每个项目调用python脚本。

但是,当一个脚本完成时,它也会最终完成所有其他脚本,并以ford循环中所有项目的相同“数据”继续stdout。

如何防止这种情况发生并让stdout等待适当的子进程完成?

更新:

以上操作是通过以下命令触发的:

const winston = require('../../server/winston');
const perform_vrp = require('../../server/modules/vrp');
const moment = require('moment');
const Utils = require('../modules/utils');
let utils = new Utils();

let do_vrp = async function(next_delivery_date, cities) {
  winston.info('Generating routes for %s', next_delivery_date);
  for (let index in cities) {
    if (cities.hasOwnProperty(index)) {
      let city = cities[index];
      winston.info('Generating route for %s on %s', city, next_delivery_date);
      await perform_vrp(next_delivery_date,[city]);
      await utils.sleep(60000);
    }
  }
};


let process_routes = async function() {

  //...

  let morning_cities = ['Boston','Chicago'];
  await do_vrp(next_delivery_date.format('YYYY-MM-DD'), morning_cities);
};

process_routes();

1 个答案:

答案 0 :(得分:1)

问题是您在for循环中使用await等待脚本完成,但是没有将脚本的执行包装在 Promise 中。

perform_tsp

扭曲生成器和事件,以解决 data 事件触发时的发生,并使用接收到的数据进行解决,如下所示:

  let perform_tsp = function () {
  //..
  return new Promise((resolve, reject) => {
    const pyProg = spawn('python3', [process.env.PWD + '/server/vrp_solver/tsp_solver.py', '/../../route_data/' + depot.city + '/' + moment(route.date_driven).format('Y-MM-DD'), 'morning', route.name]);
    winston.info('Solving the TSP for %s...', route.name);
    pyProg.stdout.on('data', function (data) {
      resolve(data);
    })
  });
}

perform_vrp

只需从perform_tsp返回返回的承诺,那么您就可以在for循环中等待它:

let perform_vrp = function () {
  //..
  return perform_tsp();
}

do_vrp

接收已解决的承诺的数据(即生成的数据),然后您可以执行 data 事件回调中的逻辑:

 let do_vrp = async function (next_delivery_date, cities) {
  winston.info('Generating routes for %s', next_delivery_date);
  for (let index in cities) {
    if (cities.hasOwnProperty(index)) {
      let city = cities[index];
      winston.info('Generating route for %s on %s', city, next_delivery_date);
      let data = await perform_vrp(next_delivery_date, [city]);
      let result_string = data.toString();
      winston.info('Route result for %s is: %s', route.name, result_string);
      let result_array = eval(result_string);
      await utils.sleep(60000);
    }
  }
};

如果您想使用 Promise.all api并行重生成卵,则先前的结构将按顺序运行卵。

  let do_vrp = async function (next_delivery_date, cities) {
  winston.info('Generating routes for %s', next_delivery_date);

  const promises = cities.map((city) => {
    return perform_vrp(next_delivery_date, [city]);
  });

  const results = await Promise.all(promises);
  results.forEach((data) => {
    winston.info('Generating route for %s on %s', city, next_delivery_date);
    let result_string = data.toString();
    winston.info('Route result for %s is: %s', route.name, result_string);
    let result_array = eval(result_string);
  });
};