gpt4 book ai didi

Python 多处理问题/误解

转载 作者:行者123 更新时间:2023-11-28 22:05:04 26 4
gpt4 key购买 nike

我遇到了多处理问题;我在 linux 2.6.36 上使用 python2.7。我知道使用更高级别的模块或库会容易得多,但我正在尝试使用较低级别的函数(os.fork() 和 os.exec*)以确保我真正理解 - 这是一种学习练习.

下面是我的代码,它是一个多处理“ping”实用程序。问题是虽然它似乎在运行,但它经常在 os.wait() 行上抛出一个 OSError。 “没有子进程”

这对我来说没有意义,因为 os.wait() 只应在程序捕捉到子进程已退出并需要回收的信号时调用。

代码后面是示例输出。

我做错了什么?

#!/usr/bin/python2.7

import os
import time
import sys
import signal

kids = []

def chldClean(SIG, FRM):
global kids
pid, status = os.wait()
kids.pop(kids.index(pid))

signal.signal(signal.SIGCHLD, chldClean)

hosts = ( '10.98.232.66',
'10.86.144.241',
'10.86.144.242',
'10.98.193.130',
'10.98.198.130',
'10.86.116.161',
'10.86.120.161',
'10.99.21.254',
'10.97.98.102',
'10.97.111.180' )


while True:
for HOST in hosts:
while len(kids) > 7:
time.sleep(0.1)
pid = os.fork()
if pid == 0:
os.closerange(0, 4)
os.execl('/bin/ping', 'PING', '-c', '1', '-w', '2', HOST)
else:
kids.append(pid)
print kids

下面有错误的输出示例----------------------------------------

[18188]
[18188, 18189]
[18188, 18189, 18190]
[18188, 18189, 18190, 18191]
[18188, 18189, 18190, 18191, 18192]
[18188, 18189, 18190, 18191, 18192, 18193]
[18188, 18189, 18190, 18191, 18192, 18193, 18194]
[18188, 18189, 18190, 18191, 18192, 18193, 18194, 18195]
[18188, 18189, 18191, 18192, 18193, 18194, 18195, 18196]
[18188, 18191, 18192, 18193, 18194, 18195, 18196, 18197]
[18188, 18191, 18192, 18194, 18195, 18196, 18197, 18198]
[18191, 18192, 18194, 18195, 18196, 18197, 18198, 18201]
[18191, 18194, 18195, 18196, 18197, 18198, 18201, 18202]
[18191, 18195, 18196, 18197, 18198, 18201, 18202, 18203]
[18195, 18196, 18197, 18198, 18201, 18202, 18203, 18204]
[18196, 18197, 18198, 18202, 18203, 18204, 18205]
[18196, 18197, 18198, 18202, 18203, 18204, 18205, 18206]
[18197, 18198, 18202, 18203, 18204, 18205, 18206, 18207]
[18198, 18203, 18204, 18205, 18206, 18207, 18210]
[18198, 18203, 18204, 18205, 18206, 18207, 18210, 18211]
Traceback (most recent call last):
File "./sunmon-mp", line 33, in <module>
pid = os.fork()
File "./sunmon-mp", line 12, in chldClean
pid, status = os.wait()
OSError: [Errno 10] No child processes
[18203, 18204, 18205, 18206, 18207, 18210, 18211, 18212]
[18203, 18204, 18206, 18207, 18210, 18211, 18212, 18213]
[18203, 18204, 18206, 18207, 18211, 18212, 18213, 18214]
[18203, 18204, 18206, 18207, 18211, 18212, 18214, 18215]
[18203, 18204, 18206, 18207, 18212, 18214, 18215, 18217]
[18203, 18204, 18206, 18207, 18214, 18215, 18217, 18218]
[18203, 18204, 18206, 18207, 18215, 18217, 18218, 18219]
[18204, 18206, 18207, 18215, 18217, 18218, 18219, 18220]
[18204, 18206, 18207, 18217, 18218, 18219, 18220, 18221]
[18206, 18207, 18217, 18218, 18219, 18220, 18221, 18223]
[18207, 18217, 18218, 18219, 18220, 18221, 18223, 18224]
[18217, 18218, 18219, 18220, 18221, 18223, 18224, 18225]
[18217, 18219, 18220, 18221, 18223, 18224, 18225, 18226]
[18217, 18219, 18220, 18221, 18223, 18225, 18226, 18227]
[18217, 18219, 18220, 18221, 18223, 18226, 18227, 18228]
[18217, 18220, 18221, 18223, 18226, 18227, 18228, 18229]
[18217, 18220, 18221, 18223, 18227, 18228, 18229, 18230]
[18217, 18220, 18221, 18223, 18227, 18228, 18230, 18231]
[18220, 18221, 18223, 18227, 18228, 18230, 18231, 18233]
[18221, 18223, 18227, 18228, 18230, 18231, 18233, 18234]
[18223, 18227, 18228, 18230, 18231, 18233, 18234, 18235]
[18223, 18227, 18228, 18231, 18233, 18234, 18235, 18236]
[18223, 18227, 18228, 18231, 18233, 18234, 18236, 18237]
[18223, 18227, 18228, 18231, 18233, 18234, 18237, 18239]
[18227, 18228, 18231, 18233, 18234, 18237, 18239, 18240]
[18228, 18231, 18233, 18234, 18237, 18239, 18240, 18241]
[18228, 18231, 18233, 18237, 18239, 18240, 18241, 18242]
[18231, 18233, 18237, 18239, 18240, 18241, 18242, 18243]
[18231, 18233, 18239, 18240, 18241, 18242, 18243, 18244]
[18231, 18233, 18239, 18240, 18242, 18243, 18244, 18245]
[18231, 18233, 18239, 18242, 18243, 18244, 18245, 18246]
[18231, 18233, 18242, 18243, 18244, 18245, 18246, 18247]
[18233, 18242, 18243, 18244, 18245, 18246, 18247, 18248]
[18242, 18243, 18244, 18245, 18246, 18247, 18248, 18249]
[18243, 18244, 18245, 18246, 18247, 18248, 18249, 18250]
[18243, 18245, 18246, 18247, 18248, 18249, 18250, 18251]
[18243, 18245, 18247, 18248, 18249, 18250, 18251, 18252]
[18243, 18245, 18248, 18249, 18250, 18251, 18252, 18253]
[18243, 18245, 18249, 18250, 18251, 18252, 18253, 18254]
[18243, 18245, 18249, 18250, 18252, 18253, 18254, 18255]
[18245, 18249, 18250, 18252, 18253, 18254, 18255, 18258]
[18249, 18250, 18252, 18253, 18254, 18255, 18258, 18259]
[18249, 18250, 18253, 18254, 18255, 18258, 18259, 18260]
[18249, 18250, 18253, 18254, 18255, 18258, 18260, 18261]
[18249, 18250, 18253, 18254, 18255, 18260, 18261, 18262]
[18250, 18253, 18254, 18255, 18260, 18261, 18262, 18263]
[18253, 18254, 18255, 18260, 18261, 18262, 18263, 18264]
[18253, 18254, 18255, 18261, 18262, 18263, 18264, 18265]
[18253, 18254, 18255, 18261, 18262, 18264, 18265, 18266]
[18254, 18255, 18261, 18262, 18264, 18265, 18266, 18267]
[18255, 18261, 18262, 18264, 18265, 18266, 18267, 18268]
[18261, 18262, 18264, 18265, 18266, 18267, 18268, 18269]
[18261, 18262, 18265, 18266, 18267, 18268, 18269, 18270]
[18261, 18262, 18265, 18266, 18267, 18268, 18270, 18271]
[18261, 18262, 18265, 18266, 18267, 18270, 18271, 18273]
[18261, 18262, 18265, 18266, 18270, 18271, 18273, 18274]
[18261, 18262, 18265, 18266, 18271, 18273, 18274, 18275]
[18261, 18262, 18265, 18266, 18271, 18273, 18275, 18276]
[18262, 18265, 18266, 18271, 18273, 18275, 18276, 18277]
[18262, 18265, 18266, 18273, 18275, 18276, 18277, 18278]
[18265, 18266, 18273, 18276, 18277, 18278, 18280]
[18265, 18266, 18273, 18276, 18277, 18278, 18280, 18281]
Traceback (most recent call last):
File "./sunmon-mp", line 33, in <module>
pid = os.fork()
File "./sunmon-mp", line 12, in chldClean
pid, status = os.wait()
OSError: [Errno 10] No child processes
[18265, 18273, 18276, 18277, 18278, 18280, 18282]
[18265, 18276, 18277, 18278, 18280, 18281, 18282, 18283]
[18265, 18276, 18278, 18281, 18282, 18283, 18284]
[18265, 18276, 18278, 18281, 18282, 18283, 18284, 18285]
[18265, 18276, 18278, 18282, 18283, 18284, 18285, 18286]
[18265, 18276, 18278, 18283, 18284, 18286, 18289]
[18265, 18276, 18278, 18283, 18284, 18286, 18289, 18290]
Traceback (most recent call last):
File "./sunmon-mp", line 33, in <module>
pid = os.fork()
File "./sunmon-mp", line 12, in chldClean
pid, status = os.wait()
OSError: [Errno 10] No child processes
[18265, 18276, 18278, 18283, 18284, 18289, 18290, 18291]
[18276, 18278, 18283, 18284, 18289, 18290, 18291, 18292]
[18276, 18278, 18283, 18284, 18290, 18291, 18292, 18293]
[18276, 18278, 18283, 18284, 18290, 18291, 18293, 18294]
[18276, 18278, 18283, 18284, 18290, 18291, 18294, 18295]
[18278, 18283, 18284, 18290, 18291, 18294, 18295, 18297]
[18283, 18284, 18290, 18291, 18294, 18295, 18297, 18298]
[18283, 18284, 18290, 18291, 18295, 18297, 18298, 18299]

最佳答案

也许您正受到 bug causing child processes to inherit pending signals 的影响.这可以解释为什么堆栈跟踪出现不止一次。 child 试图等待自己不存在的 child 。

同时可能会同时处理多个排队的相同类型信号,因此我不建议在信号处理程序中使用 wait()。

关于Python 多处理问题/误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5783183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com