简体   繁体   English

Python等待子进程完成

[英]Python waiting for subprocess to finish

I'm really new to Python and I got a little problem with the subprocess class. 我对Python真的很陌生,并且子流程类有一些问题。

I'm starting an external Program with : 我正在使用来启动外部程序:

      thread1.event.clear()
      thread2.event.clear()
      print "Sende Motoren STOP"
      print "Gebe BILD in Auftrag"
      proc = Popen(['gphoto2 --capture-image &'], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
      sleep (args.max+2)
      thread1.event.set()
      thread2.event.set()
      sleep (args.tp-2-args.max)

My Problem is that in my shell where I Started the Python script, I still get the outputs of GPHOTO2 and I think Python is still waiting for GPHOTO to finish. 我的问题是,在我启动Python脚本的shell中,我仍然获得GPHOTO2的输出,并且我认为Python仍在等待GPHOTO完成。

Any ideas? 有任何想法吗?

The documentation for subprocess.Pope states: subprocess.Pope的文档指出:

stdin, stdout and stderr specify the executed programs' standard
input, standard output and standard error file handles, respectively.
[...]
With None, no redirection will occur; the child's file handles will be
inherited from the parent.

So you might want to try something along the lines of this. 因此,您可能想尝试一些类似的方法。 Which btw. 顺便说一句。 blocks until completion. 直到完成为止。 So you might not need the sleep() (here the wait() from subprocess.Popen might be want you want?). 因此,您可能不需要sleep() (这里可能需要subprocess.Popen中的wait()吗?)。

import subprocess

ret_code = subprocess.call(["echo", "Hello World!"], stdout=subprocess.PIPE);

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

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