Python 在groovy中执行多个子进程失败

Python 在groovy中执行多个子进程失败,python,groovy,process,Python,Groovy,Process,我需要创建一个调用应用程序(c++二进制)4000次的脚本。应用程序接受一些参数,并为每个调用将一个zip文件写入磁盘。因此,当脚本执行时,4000个zip文件将被写入磁盘。该应用程序支持多线程 我首先创建了一个bash脚本来完成这项工作,它工作得很好。但是现在我需要脚本独立于平台。因此,我尝试将脚本移植到groovy,如下所示: for (int i = 1; i <= 4000; i++) { def command = """myExecutable

我需要创建一个调用应用程序(c++二进制)4000次的脚本。应用程序接受一些参数,并为每个调用将一个zip文件写入磁盘。因此,当脚本执行时,4000个zip文件将被写入磁盘。该应用程序支持多线程

我首先创建了一个bash脚本来完成这项工作,它工作得很好。但是现在我需要脚本独立于平台。因此,我尝试将脚本移植到groovy,如下所示:

for (int i = 1; i <= 4000; i++) {
  def command = """myExecutable
                 a=$argA
                 b=$outDir"""

  def proc = command.execute()                 // Call *execute* on the string
  proc.waitFor()                               // Wait for the command to finish
  // Obtain status and output
  println "return code: ${ proc.exitValue()}"
  println "stderr: ${proc.err.text}"
  println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

  println "iteration : " + i

}
import groovyx.gpars.GParsPool

GParsPool.withPool(8){ // Start in pool with 8 threads.
  (1..4000).toList().eachParallel {
    def p = "myExecutable a=$argA b=$outDir".execute()
    def sout = new StringBuffer();
    def serr = new StringBuffer();
    p.waitForProcessOutput(sout, serr)
    synchronized (System.out) {
      println "return code: ${ p.exitValue()}"
      println "stderr: $serr"
      println "stdout: $sout"
      println "iteration $it"
    }
  }
}

对于(int i=1;i是,您应该关闭属于进程的流

或者,如@tim_yates所说,您应该使用ConsumerProcessOutput,或者在Concurrent解决方案中,使用waitForProcessOutput,这将为您关闭它们

对于并行计算,您可以使用smth。如下所示:

for (int i = 1; i <= 4000; i++) {
  def command = """myExecutable
                 a=$argA
                 b=$outDir"""

  def proc = command.execute()                 // Call *execute* on the string
  proc.waitFor()                               // Wait for the command to finish
  // Obtain status and output
  println "return code: ${ proc.exitValue()}"
  println "stderr: ${proc.err.text}"
  println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

  println "iteration : " + i

}
import groovyx.gpars.GParsPool

GParsPool.withPool(8){ // Start in pool with 8 threads.
  (1..4000).toList().eachParallel {
    def p = "myExecutable a=$argA b=$outDir".execute()
    def sout = new StringBuffer();
    def serr = new StringBuffer();
    p.waitForProcessOutput(sout, serr)
    synchronized (System.out) {
      println "return code: ${ p.exitValue()}"
      println "stderr: $serr"
      println "stdout: $sout"
      println "iteration $it"
    }
  }
}

是的,您应该关闭属于进程的流

或者,如@tim_yates所说,您应该使用ConsumerProcessOutput,或者在Concurrent解决方案中,使用waitForProcessOutput,这将为您关闭它们

对于并行计算,您可以使用smth。如下所示:

for (int i = 1; i <= 4000; i++) {
  def command = """myExecutable
                 a=$argA
                 b=$outDir"""

  def proc = command.execute()                 // Call *execute* on the string
  proc.waitFor()                               // Wait for the command to finish
  // Obtain status and output
  println "return code: ${ proc.exitValue()}"
  println "stderr: ${proc.err.text}"
  println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

  println "iteration : " + i

}
import groovyx.gpars.GParsPool

GParsPool.withPool(8){ // Start in pool with 8 threads.
  (1..4000).toList().eachParallel {
    def p = "myExecutable a=$argA b=$outDir".execute()
    def sout = new StringBuffer();
    def serr = new StringBuffer();
    p.waitForProcessOutput(sout, serr)
    synchronized (System.out) {
      println "return code: ${ p.exitValue()}"
      println "stderr: $serr"
      println "stdout: $sout"
      println "iteration $it"
    }
  }
}

是的,您应该关闭属于进程的流

或者,如@tim_yates所说,您应该使用ConsumerProcessOutput,或者在Concurrent解决方案中,使用waitForProcessOutput,这将为您关闭它们

对于并行计算,您可以使用smth。如下所示:

for (int i = 1; i <= 4000; i++) {
  def command = """myExecutable
                 a=$argA
                 b=$outDir"""

  def proc = command.execute()                 // Call *execute* on the string
  proc.waitFor()                               // Wait for the command to finish
  // Obtain status and output
  println "return code: ${ proc.exitValue()}"
  println "stderr: ${proc.err.text}"
  println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

  println "iteration : " + i

}
import groovyx.gpars.GParsPool

GParsPool.withPool(8){ // Start in pool with 8 threads.
  (1..4000).toList().eachParallel {
    def p = "myExecutable a=$argA b=$outDir".execute()
    def sout = new StringBuffer();
    def serr = new StringBuffer();
    p.waitForProcessOutput(sout, serr)
    synchronized (System.out) {
      println "return code: ${ p.exitValue()}"
      println "stderr: $serr"
      println "stdout: $sout"
      println "iteration $it"
    }
  }
}

是的,您应该关闭属于进程的流

或者,如@tim_yates所说,您应该使用ConsumerProcessOutput,或者在Concurrent解决方案中,使用waitForProcessOutput,这将为您关闭它们

对于并行计算,您可以使用smth。如下所示:

for (int i = 1; i <= 4000; i++) {
  def command = """myExecutable
                 a=$argA
                 b=$outDir"""

  def proc = command.execute()                 // Call *execute* on the string
  proc.waitFor()                               // Wait for the command to finish
  // Obtain status and output
  println "return code: ${ proc.exitValue()}"
  println "stderr: ${proc.err.text}"
  println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

  println "iteration : " + i

}
import groovyx.gpars.GParsPool

GParsPool.withPool(8){ // Start in pool with 8 threads.
  (1..4000).toList().eachParallel {
    def p = "myExecutable a=$argA b=$outDir".execute()
    def sout = new StringBuffer();
    def serr = new StringBuffer();
    p.waitForProcessOutput(sout, serr)
    synchronized (System.out) {
      println "return code: ${ p.exitValue()}"
      println "stderr: $serr"
      println "stdout: $sout"
      println "iteration $it"
    }
  }
}

可能是输出流阻塞:

(1..<4000).each { i ->
    println "iteration : $i"

    def command = """myExecutable
                 a=$argA
                 b=$outDir"""

    def proc = command.execute()
    // Consume the outputs from the process and pipe them to our output streams
    proc.consumeProcessOutput( System.out, System.err )

    // Wait for the command to finish
    proc.waitFor()

    // Obtain status
    println "return code: ${proc.exitValue()}"
}
(1。。
println“迭代:$i”
def command=“”myExecutable
a=$argA
b=$outDir”“”
def proc=command.execute()
//使用流程的输出,并将它们输送到我们的输出流
进程consumeProcessOutput(System.out、System.err)
//等待命令完成
waitFor()程序
//获得地位
println“返回代码:${proc.exitValue()}”
}

可能是输出流阻塞:

(1..<4000).each { i ->
    println "iteration : $i"

    def command = """myExecutable
                 a=$argA
                 b=$outDir"""

    def proc = command.execute()
    // Consume the outputs from the process and pipe them to our output streams
    proc.consumeProcessOutput( System.out, System.err )

    // Wait for the command to finish
    proc.waitFor()

    // Obtain status
    println "return code: ${proc.exitValue()}"
}
(1。。
println“迭代:$i”
def command=“”myExecutable
a=$argA
b=$outDir”“”
def proc=command.execute()
//使用流程的输出,并将它们输送到我们的输出流
进程consumeProcessOutput(System.out、System.err)
//等待命令完成
waitFor()程序
//获得地位
println“返回代码:${proc.exitValue()}”
}

可能是输出流阻塞:

(1..<4000).each { i ->
    println "iteration : $i"

    def command = """myExecutable
                 a=$argA
                 b=$outDir"""

    def proc = command.execute()
    // Consume the outputs from the process and pipe them to our output streams
    proc.consumeProcessOutput( System.out, System.err )

    // Wait for the command to finish
    proc.waitFor()

    // Obtain status
    println "return code: ${proc.exitValue()}"
}
(1。。
println“迭代:$i”
def command=“”myExecutable
a=$argA
b=$outDir”“”
def proc=command.execute()
//使用流程的输出,并将它们输送到我们的输出流
进程consumeProcessOutput(System.out、System.err)
//等待命令完成
waitFor()程序
//获得地位
println“返回代码:${proc.exitValue()}”
}

可能是输出流阻塞:

(1..<4000).each { i ->
    println "iteration : $i"

    def command = """myExecutable
                 a=$argA
                 b=$outDir"""

    def proc = command.execute()
    // Consume the outputs from the process and pipe them to our output streams
    proc.consumeProcessOutput( System.out, System.err )

    // Wait for the command to finish
    proc.waitFor()

    // Obtain status
    println "return code: ${proc.exitValue()}"
}
(1。。
println“迭代:$i”
def command=“”myExecutable
a=$argA
b=$outDir”“”
def proc=command.execute()
//使用流程的输出,并将它们输送到我们的输出流
进程consumeProcessOutput(System.out、System.err)
//等待命令完成
waitFor()程序
//获得地位
println“返回代码:${proc.exitValue()}”
}

你应该使用
ConsumerProcessOutput
而不是
p.err.text
p.out.text
,因为它们会阻塞。谢谢你的通知!是的,我错过了它。但是如果我们使用GPAR和Concurence,我们就有一个,所以我们应该像我想的那样使用waitForProcessOutput。u123要求concurent解决方案。我说得对吗?你应该使用
consumeProcessOutput
而不是
p.err.text
p.out.text
,因为它们可以阻止。谢谢通知!是的,我错过了它。但是如果我们使用GPAR和Concurence,我们有一个,所以我们应该使用waitForProcessOutput,就像我想的那样。u123要求concurent解决方案。我说的对吗?你应该使用
ConsumerprocessOutput
而不是than
p.err.text
p.out.text
,因为它们可以阻止。谢谢你的通知!是的,我错过了。但是如果我们使用GPAR和Concurrency,我们有一个,所以我们应该使用waitForProcessOutput,就像我想的那样。u123要求Concurrent解决方案。我说的对吗?你应该使用
ConsumerProcessOutput
而不是
p.err.text
p.out.text
,因为这些会阻塞谢谢通知!是的,我错过了它。但是如果我们使用GPAR和Concurence,我们有一个,所以我们应该使用waitForProcessOutput,正如我所想。u123要求concurent解决方案。我说的对吗?