os.kill 没有引发 OSError,但是我没有看到给定的 pid 正在运行
在我的 ubuntu 服务器上,我运行以下命令:
python -c 'import os; os.kill(5555, 0)'
这样做是为了查看 pid 5555 是否正在运行。根据我的理解,如果 pid 没有运行,这应该会引发 OSError 。这不会对我引发 OSError,这意味着它应该是一个正在运行的进程。但是,当我运行时:
ps aux | grep 5555
我看到没有使用该 pid 运行的进程。这种情况也会发生在该一般范围内的其他几个 pid 上,但不会发生在 555 或 55555 上。
有谁知道为什么 os.kill 不会像预期的那样引发 OSError 吗?
注意:这是在 python 2.5.1 下运行的。
On my ubuntu server I run the following command:
python -c 'import os; os.kill(5555, 0)'
This is done so that I can see if pid 5555 is running. From my understanding this should raise an OSError if the pid is not running. This is not raising an OSError for me which means it should be a running process. However when I run:
ps aux | grep 5555
I see no process running with that pid. This also happens on several other pids in that general range, but it does not happen with say 555 or 55555.
Does anyone have any insight as to why os.kill would not raise an OSError like it is expected to?
Note: this is running under python 2.5.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在linux下,每个进程和每个线程都有不同的pid。
os.kill
并不关心您是否有线程 pid,或者任务 pid,但是ps
通常不会显示线程 pid。例如,在我的机器上,PID 为 8502 的进程正在运行线程,您可以像这样看到
注意,8503 没有出现在进程列表中
,但是使用更多
ps
参数您可以看到它(Grepping for
[8]503
意味着grep
不会出现 - 这是一个古老的 UNIX 技巧!)现在让我们看看它是否还活着
这会重复您的问题。
我想如果你这样做
或者
你会看到罪魁祸首。
Under linux, each process and each thread have a different pid.
os.kill
doesn't care whether you have a thread pid, or a task pid, howeverps
doesn't normally show the thread pids.For instance on my machine the process with PID 8502 is running threads which you can see like this
Note that 8503 doesn't appear in the process list
However using some more
ps
arguments you can see it(Grepping for
[8]503
means that thegrep
won't show up - it's an old unix trick!)Now lets see if it is alive or not
This duplicates your problem.
I think if you do
or
You will see the culprit thread.
尝试安装 htop (sudo apt-get install htop),它有时会显示 ps 不显示的进程。
Try installing htop (sudo apt-get install htop), it sometimes displays process that ps doesn't.
也许这是2.5的一个错误?在 2.6.4 上我得到:
我相信,有一个错误报告:
http://mail.python.org/pipermail/new-bugs-announce/2009-February/004222.html
Maybe it's a bug in 2.5? On 2.6.4 I get:
I believe, there is a bug report for this:
http://mail.python.org/pipermail/new-bugs-announce/2009-February/004222.html
我不知道为什么在某些情况下不会引发 OSError,但值得注意的是,在基于 linux 和 unix 的操作系统上有一个最大 pid 值:
I don't know why that OSError is not raised in some cases, but it's important to note that there is a max pid value on linux and unix based OS: