0

我查看了之前提出的问题,但没有找到类似的问题。我错过了一个,请告诉我,我很抱歉再次问同样的问题。

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

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

如果我改用“调用”阻塞方法,也会发生这种情况。此问题目前仅在此一个节点上发生。它运行 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)                           = ?

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

任何帮助将不胜感激。

先感谢您,

D.

PS。如果有人想要任一节点的完整 strace 输出,请告诉我。我只能在星期一之前让这台机器保持这种状态:-(

4

1 回答 1

1

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

你想要的是subprocess.check_output().

于 2012-12-06T23:26:18.103 回答