运行 subprocess.call 来运行 Cocoa 命令行应用程序

发布于 2024-09-10 20:05:03 字数 910 浏览 0 评论 0原文

我编写了一段 Cocoa 代码,它接收一个包含边界框的 XML 文件,然后将其绘制在视频顶部(每个框都有一个关联的帧)。 Cocoa 程序旨在从命令行运行(并将其所有参数作为命令行参数),

我可以使用任何 XML 文档运行程序。但是,当我尝试从 Python 脚本中运行该程序时,遇到了问题。例如:

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)

有时这会导致我的“cval”失败,有时则不会(更改 XML 文档中的一个数字可以改变其行为)。当尝试读取不存在的 XML 元素时,我还可以验证它是否损坏。只是,我可以打开“test.xml”,并验证该元素确实存在。

但是,如果我自己使用“test.xml”运行“cval”(在 Python 脚本之外),则它可以正常工作。这让我相信当我执行“subprocess.call”时会发生一些奇怪的事情,但我不确定它可能是什么。我还有其他 Cocoa/Python 混合,它们执行完全不同的任务(即不使用 XML),它们也任意表现出奇怪的行为,但本质上更复杂。

我希望有人也可能遇到这个问题,或者可能知道调试这个奇怪现象的下一步。

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). The Cocoa program is meant to be run from the command line (and takes in all its parameters as command line arguments)

I can run program just fine with any XML document. However, I run into problems when I try to run the program from within a Python script. 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). I can also verify it's breaking when trying to read an XML element that isn't there. Only, I can open up 'test.xml', and verify the element does in fact exist.

However, if I then run 'cval' myself (outside of the Python script) with 'test.xml', it works fine. 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. I have other Cocoa/Python mixes that do completely different tasks (i.e. not using XML) that also arbitrarily exhibit weird behavior, but are more complex in nature.

I was hoping someone might have run into this problem as well, or might know the next step in debugging this weirdness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-09-17 20:05:03

由于代码最初使用临时文件,因此在将文件传递给子进程之前我无法关闭该文件。但是,我应该做的是在调用 subprocess.call 之前刷新文件。不一致的行为可能是由于输入的大小导致不同阈值下的自动刷新造成的。

代码应为:

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)

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. 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)
奈何桥上唱咆哮 2024-09-17 20:05:03

当 subprocess.call 的返回码指示错误时,也许可以尝试在其中放置一个“打印命令”语句。失败时,查看子进程执行的内容与您可能从命令行运行的内容之间是否有任何差异。另外,尝试调用 subprocess.call(command, shell=True) ,以便您的命令像在 shell 中一样执行(使用字符串格式等)。

Perhaps try placing a "print command" statement in there, when the return code of subprocess.call indicates an error. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文