Python subprocess.Popen挂起

时间:2012-12-06 20:51:12

标签: python subprocess hang

我查看了之前提出的问题并没有找到类似的问题。我错过了一个,请让我知道,我为再次提出同样的问题而道歉。

我正在调试执行以下代码时遇到Popen挂起的问题(python 2.6):

#!/usr/bin/env python
import subprocess
cmd = 'service drbd start'
rc  = subprocess.call(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

如果我使用“call”阻止方法,也会发生这种情况。此问题仅在此一个节点上发生。它运行Suse SLES 11 SP1。

失败节点上的strace显示以下内容:

<snip>
fstat(5, {st_mode=S_IFREG|0644, st_size=22808, ...}) = 0
open("/usr/lib64/python2.6/lib-dynload/cStringIO.so", O_RDONLY) = 6
read(6, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\34\0\0\0\0\0\0"..., 832) =832
fstat(6, {st_mode=S_IFREG|0644, st_size=22808, ...}) = 0
mmap(NULL, 2117912, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 6, 0) = 0x7f0cd5987000
fadvise64(6, 0, 2117912, POSIX_FADV_WILLNEED) = 0
mprotect(0x7f0cd598b000, 2093056, PROT_NONE) = 0
mmap(0x7f0cd5b8a000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 6, 0x3000) = 0x7f0cd5b8a000
close(6)                                = 0
mprotect(0x7f0cd5b8a000, 4096, PROT_READ) = 0
close(5)                                = 0
close(4)                                = 0
getrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=1024}) = 0
close(3)                                = 0
pipe([3, 4])                            = 0
pipe([5, 6])                            = 0
fcntl(6, F_GETFD)                       = 0
fcntl(6, F_SETFD, FD_CLOEXEC)           = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f0cd793e9d0) = 27688
close(6)                                = 0
close(4)                                = 0
mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0cd5886000
read(5, "", 1048576)                    = 0
mremap(0x7f0cd5886000, 1052672, 4096, MREMAP_MAYMOVE) = 0x7f0cd5886000
close(5)                                = 0
munmap(0x7f0cd5886000, 4096)            = 0
fcntl(3, F_GETFL)                       = 0 (flags O_RDONLY)
fstat(3, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0cd794a000
lseek(3, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
fstat(3, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
munmap(0x7f0cd794a000, 4096)            = 0
wait4(27688,

它就在这里。相应的流程是:

root     27688 27659  0 15:34 pts/2    00:00:00 /bin/sh /sbin/service drbd start
root     27690 27688  0 15:34 pts/2    00:00:00 /bin/bash /etc/init.d/drbd start

以下是同一代码工作的另一个节点上的输出:

<snip>
fstat(5, {st_mode=S_IFREG|0644, st_size=22808, ...}) = 0
open("/usr/lib64/python2.6/lib-dynload/cStringIO.so", O_RDONLY) = 6
read(6, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\34\0\0\0\0\0\0"..., 832) = 832
fstat(6, {st_mode=S_IFREG|0644, st_size=22808, ...}) = 0
mmap(NULL, 2117912, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 6, 0) = 0x7f57b3833000
fadvise64(6, 0, 2117912, POSIX_FADV_WILLNEED) = 0
mprotect(0x7f57b3837000, 2093056, PROT_NONE) = 0
mmap(0x7f57b3a36000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 6, 0x3000) = 0x7f57b3a36000
close(6)                                = 0
mprotect(0x7f57b3a36000, 4096, PROT_READ) = 0
close(5)                                = 0
close(4)                                = 0
getrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=1024}) = 0
close(3)                                = 0
pipe([3, 4])                            = 0
pipe([5, 6])                            = 0
fcntl(6, F_GETFD)                       = 0
fcntl(6, F_SETFD, FD_CLOEXEC)           = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f57b57ea9d0) = 32277
close(6)                                = 0
close(4)                                = 0
mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f57b3732000
read(5, "", 1048576)                    = 0
mremap(0x7f57b3732000, 1052672, 4096, MREMAP_MAYMOVE) = 0x7f57b3732000
close(5)                                = 0
munmap(0x7f57b3732000, 4096)            = 0
fcntl(3, F_GETFL)                       = 0 (flags O_RDONLY)
fstat(3, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f57b57f6000
lseek(3, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
fstat(3, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
munmap(0x7f57b57f6000, 4096)            = 0
wait4(32277, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 32277
--- SIGCHLD (Child exited) @ 0 (0) ---
close(3)                                = 0
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7f57b50386a0}, {0x7f57b52d88f8, [], SA_RESTORER, 0x7f57b50386a0}, 8) = 0
exit_group(0)                           = ?

我无法弄清楚这个节点有什么问题?为什么这个命令挂在第一个节点而不是第二个节点上(运行完全相同的OS和python版本)?

非常感谢任何帮助。

提前谢谢你,

d

PS。如果有人想从任一节点获得完整的strace输出,请告诉我。我被允许将这台机器保持在这种状态,直到星期一: - (

1 个答案:

答案 0 :(得分:1)

使用subprocess.call(...stdout=PIPE)是错误的。这会将stdout重定向到管道,但当前进程不会读取此管道,因此在输出超过N个字节的数据后,子进程将阻塞(N取决于操作系统)。

你想要的是subprocess.check_output()