简体   繁体   English

运行subprocess.call以运行可可命令行应用程序

[英]Running subprocess.call to run a Cocoa command-line application

I have one piece of Cocoa code I wrote that takes in an XML file containing bounding boxes that are then drawn on top of a video (each box has an associated frame). 我编写了一段可可代码,其中包含一个XML文件,其中包含边界框,然后将边界框绘制在视频的顶部(每个框都有一个关联的框架)。 The Cocoa program is meant to be run from the command line (and takes in all its parameters as command line arguments) Cocoa程序旨在从命令行运行(并将其所有参数作为命令行参数)

I can run program just fine with any XML document. 我可以在任何XML文档中运行程序。 However, I run into problems when I try to run the program from within a Python script. 但是,当我尝试从Python脚本中运行程序时遇到了问题。 For example: 例如:

with file("test.xml") as temp:
    temp.write(doc.toprettyxml())
    # cval is my cocoa program to call, the other arguments are given to the Python script and parsed with optparser
    command = ["./cval", "-o", options.output, "-i", str(options.interval), "-s", "%dx%d" %    (options.width, options.height), "-f", str(options.frames), "-x", temp.name]
    subprocess.call(command)

Sometimes this will cause my 'cval' to fail, other times not (changing one number in the XML document can change its behavior). 有时这会导致我的'cval'失败,有时则不行(在XML文档中更改一个数字可以更改其行为)。 I can also verify it's breaking when trying to read an XML element that isn't there. 当尝试读取不存在的XML元素时,我还可以验证它是否损坏。 Only, I can open up 'test.xml', and verify the element does in fact exist. 只有,我可以打开“ test.xml”,并验证元素是否确实存在。

However, if I then run 'cval' myself (outside of the Python script) with 'test.xml', it works fine. 但是,如果我随后使用“ test.xml”自己(在Python脚本之外)运行“ cval”,则效果很好。 This leads me to believe that there is something strange happening when I do 'subprocess.call', but I'm not sure what it could be. 这使我相信,当我执行“ subprocess.call”时,会发生一些奇怪的事情,但是我不确定会发生什么。 I have other Cocoa/Python mixes that do completely different tasks (ie not using XML) that also arbitrarily exhibit weird behavior, but are more complex in nature. 我还有其他的Cocoa / Python混合物,它们执行完全不同的任务(即不使用XML),它们也任意表现出怪异的行为,但本质上更为复杂。

I was hoping someone might have run into this problem as well, or might know the next step in debugging this weirdness. 我希望有人也可能遇到此问题,或者可能知道调试此怪异步骤的下一步。

Because the code originally used temporary files, I couldn't close the file before passing it to the subprocess. 由于代码最初使用的是临时文件,因此在将文件传递给子流程之前,我无法关闭文件。 However, what I should have done instead is to flush the file before subprocess.call was invoked. 但是,我应该做的是在调用subprocess.call之前刷新文件。 The inconsistent behavior likely resulted from the size of input causing automatic flushing at different thresholds. 不一致的行为很可能是由于输入大小导致在不同阈值处自动刷新所致。

The code should read: 该代码应显示为:

with file("test.xml") as temp:
    temp.write(doc.toprettyxml())
    temp.flush()
    command = ["./cval", "-o", options.output, "-i", str(options.interval), "-s", "%dx%d" %    (options.width, options.height), "-f", str(options.frames), "-x", temp.name]
    subprocess.call(command)

Perhaps try placing a "print command" statement in there, when the return code of subprocess.call indicates an error. subprocess.call的返回代码指示错误时,也许尝试在其中放置“打印命令”语句。 On failure, see if there's any difference between what's being executed by subprocess and what you might run from the command line. 失败时,请查看子进程正在执行的内容与您可能从命令行运行的内容之间是否有任何区别。 Also, try calling subprocess.call(command, shell=True) , so your command is being executed as it would in the shell (with string formatting, etc). 另外,尝试调用subprocess.call(command, shell=True) ,这样您的命令将像在shell中一样被执行(使用字符串格式等)。

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

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