【问题标题】:Python Subprocess.Popen not getting exit, it getting hangedPython Subprocess.Popen 没有退出,它被挂起
【发布时间】:2013-12-11 07:37:09
【问题描述】:

我正在使用 subprocess.Popen 来调用控制台应用程序。控制台应用程序本身调用另一个子进程来执行下载操作。父进程调用子进程后退出。

在命令提示符下手动运行脚本时,我可以获得子进程的输出。

但是 subprocess.Popen 在系统环境中运行脚本时被挂起(提交后挂钩)。子进程没有退出。

 p1 = subprocess.Popen([Application,arg1, arg2, arg3], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
 Down_out = p1[0]
 Down_Err = p1[1]

提前致谢

【问题讨论】:

  • 这和svn有什么关系?
  • 当提交发生在我的 svn 存储库中时,从 post commit hook 调用的 python 脚本
  • shell=True 和列表参数几乎总是一个错误。要么删除shell=True(不理会列表参数),要么将列表参数转换为字符串(一个shell命令,因为它会出现在控制台中)。

标签: python svn subprocess post-commit-hook


【解决方案1】:

从您提供的信息中很难说,但可能arguments 是一个带有多个参数的字符串,当它们应该被拆分为列表中的多个元素时。如果将所有参数组合到一个字符串中,您正在执行的程序的行为将不会是您所期望的。

例如:

>>> from subprocess import Popen
>>> Popen(['touch', '/tmp/testing /tmp/foo']).communicate()
touch: cannot touch ‘/tmp/testing /tmp/foo’: No such file or directory
(None, None)
>>> Popen(['touch', '/tmp/testing', '/tmp/foo']).communicate()
(None, None)

在第一个中,'/tmp/testing /tmp/foo' 是一个字符串。

在第二个中,它是列表中的两个独立元素。运行正常。

我猜你的挂起是因为参数无效。

【讨论】:

  • 您能否提供您传递的确切参数,而不是 Application、arg1、arg2 等?另外,当你手动运行它时你会看到什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 2020-12-08
  • 2012-03-16
相关资源
最近更新 更多