【发布时间】:2018-01-23 22:21:42
【问题描述】:
我正在使用子进程库在 python 脚本中运行 jirashell。我目前在实时打印输出时遇到问题。当我运行 jirashell 时,它会输出信息而不是提示用户(y/n)。在我输入“y”或“n”之前,子流程不会在提示之前打印出信息。
我使用的代码是
_consumer_key = "justin-git"
_cmd = "jirashell -s {0} -od -k {1} -ck {2} -pt".format(JIRA_SERVER,
_rsa_private_key_path, _consumer_key)
p = subprocess.Popen(_cmd.split(" "), stdout=subprocess.PIPE,
stderr=subprocess.PIPE, bufsize=0)
out, err = p.communicate() # Blocks here
print out
print err
输出是这样的:
n # I enter a "n" before program will print output.
Output:
Request tokens received.
Request token: asqvavefafegagadggsgewgqqegqgqge
Request token secret: asdbresbdfbrebsaerbbsbdabweabfbb
Please visit this URL to authorize the OAuth request:
http://localhost:8000/plugins/servlet/oauth/authorize?oauth_token=zzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Have you authorized this program to connect on your behalf to http://localhost:8000? (y/n)
Error:
Abandoning OAuth dance. Your partner faceplants. The audience boos. You feel shame.
有谁知道我如何让它在提示之前打印输出而不是等待输入 y/n?请注意,我还需要能够存储命令产生的输出,这样“os.system()”就不起作用了……
编辑:
看起来在 jirashell 内部有一部分代码正在等待输入,这导致了阻塞。在将某些内容传递到此输入之前,不会输出任何内容...仍在研究如何解决此问题。我正在尝试将我需要的部分代码移动到我的应用程序中。这个解决方案看起来并不优雅,但我现在看不到任何其他方式。
approved = input(
'Have you authorized this program to connect on your behalf to {}? (y/n)'.format(server))
【问题讨论】:
标签: python subprocess pipe output stdout