2017-02-17 20 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