2017-02-17 55 views
0

我正在尝试产生子进程,并在Promise中产生结果。 子进程不能同时运行多次。返回连续运行的子进程的承诺

我在承诺中包装node.js的child_process.spawn()。 承诺在成功退出成功时履行,否则拒绝。 请求在不同时间com有时一个有时多个。我需要为每个请求执行相同的命令,甚至可以多次(使用不同或可能相同的选项)来完成请求。但是,如果并行运行,该命令将被锁定。所以如果不确定事先没有退出就不能运行。

也许我需要他们排队?

我无法把我的头围绕如何在JavaScript/Typescript中做到这一点。忙碌的等待显然不是上帝的主意,但我希望它能解释我想在这里做什么。

export class RunThingy{ 

private busy: boolean; 

constructor() { 
    this.busy = false; 
} 

private run(options: string[]): Promise<string> { 
    return new Promise((resolve, reject) => { 
    let stdout: string = ''; 
    let stderr: string = ''; 
    while (!this.busy) {    //Problem 
     this.busy = true; 
     let process: ChildProcess = spawn('processName', options); 
     process.stdout.on('data', (contents) => { stdout += contents; }); 
     process.stderr.on('data', (contents) => { stderr += contents; }); 
     process 
     .on('error', reject) 
     .on('close', function (code) { 
      if (code === 0) { 
      resolve(stdout); 
      } else { 
      reject(stderr); 
      } 
      this.buisy = false;  //Problem 
     }); 
    } 
    }); 
} 

编辑:更名为command[]options[]

+0

也许在promise中使用'.finally((=)this.buisy = false;)'? – Ioan

+0

我不明白这可能会对此有所帮助。没有try catch块。你建议什么? –

+0

'.finally'和'finally'不一样 –

回答

1

允诺可以拒绝或下不为例。每个流程都必须包含在承诺中。这是一个命题:

export class RunThingy { 
    private curCommand: Promise<string> | null 

    private run(options: string[]): Promise<string> { 
     let next: Promise<string> 
     if (this.curCommand) { 
      next = this.curCommand.then(
       () => runCommand(options), // the next command will be started after 
       () => runCommand(options) // the current will be resolved or rejected 
      ) 
     } else 
      next = runCommand(options) 
     next = next.then(stdout => { 
      if (next === this.curCommand) // if this is the last command 
       this.curCommand = null // then forget the current command 
      return stdout     // return the command value 
     }, err => { 
      if (next === this.curCommand) // if this is the last command 
       this.curCommand = null // then forget the current command 
      throw err      // throw again the error 
     }) 
     this.curCommand = next 
     return this.curCommand 
    } 
} 

function runCommand(options: string[]): Promise<string> { 
    return new Promise((resolve, reject) => { 
     let stdout = ''; 
     let stderr = ''; 
     let process: ChildProcess = spawn('processName', options); 
     process.stdout.on('data', (contents) => { stdout += contents; }); 
     process.stderr.on('data', (contents) => { stderr += contents; }); 
     process 
      .on('error', reject) 
      .on('close', function (code) { 
       if (code === 0) { 
        resolve(stdout); 
       } else { 
        reject(new Error(stderr)); 
       } 
      }); 
    }); 
} 

在该方法中run,我们检查是否有电流命令。新命令将在当前命令解决或拒绝后启动。

+0

我不明白这个解决方案如何阻止这个进程并行运行?对不起,我应该更好地称为“命令”作为“选项” –

+0

“选项”很好。但我不明白我是如何排队的过程的多个呼叫。 –

+0

@ J.Doe哦。我不明白你的几个过程是什么?你为什么不运行一次这个命令? 'while'循环的目的是什么?或者你想用相同的选项执行几次相同的命令? – Paleo