开发者

Python - Execute Process -> Block till it exits & Suppress Output

开发者 https://www.devze.com 2022-12-25 15:20 出处:网络
I\'m using the following to execute a process and hide its output from Python. It\'s in a loop though, and I need a way to block until the sub process has terminated before moving to the next iteratio

I'm using the following to execute a process and hide its output from Python. It's in a loop though, and I need a way to block until the sub process has terminated before moving to the next iteration.

subprocess.开发者_如何学编程Popen(["scanx", "--udp", host], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 


Use subprocess.call(). From the docs:

subprocess.call(*popenargs, **kwargs)
Run command with arguments. Wait for command to complete, then return the returncode attribute. The arguments are the same as for the Popen constructor.

Edit:

subprocess.call() uses wait(), and wait() is vulnerable to deadlocks (as Tommy Herbert pointed out). From the docs:

Warning: This will deadlock if the child process generates enough output to a stdout or stderr pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.

So if your command generates a lot of output, use communicate() instead:

p = subprocess.Popen(
    ["scanx", "--udp", host],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
out, err = p.communicate()


If you don't need output at all you can pass devnull to stdout and stderr. I don't know if this can make a difference but pass a bufsize. Using devnull now subprocess.call doesn't suffer of deadlock anymore

import os
import subprocess

null = open(os.devnull, 'w')
subprocess.call(['ls', '-lR'], bufsize=4096, stdout=null, stderr=null)
0

精彩评论

暂无评论...
验证码 换一张
取 消