Python多处理问题/误解 [英] Python multiprocessing issue / misunderstanding

查看:69
本文介绍了Python多处理问题/误解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having an issue with multiprocessing; I'm using python2.7 on linux 2.6.36. I know this would be much easier with a higher level module or library but I'm trying to use the lower level functions (os.fork() and os.exec*) to ensure I really understand- This is kind of a learning exercise.

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

Below is my code, it's a multiprocessing 'ping' utility. The issue is while it seems to run, it throws an OSError every so often on the os.wait() line. "No child processes"

这对我来说没有意义,因为仅当程序捕获到子进程已退出并且需要重新获得信号时才应调用os.wait().

That doesn't make sense to me as os.wait() should only be called when the program catches a signal that a child process has exited and needs to be reaped.

下面的代码是示例输出.

Following the code is sample output.

我在做什么错了?

#!/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

示例输出中的错误低于----------------------------------------

Sample Output with Errors below ----------------------------------------

[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]

推荐答案

也许您正受到导致孩子的错误的影响继承未决信号的过程.这可以解释为什么堆栈跟踪不止一次出现.这个孩子正试图等待自己不存在的孩子.

Perhaps you are being affected by a bug causing child processes to inherit pending signals. That would explain why the stack trace appears more than once. The child is trying to wait on its own non-existent child.

也可能一次处理多个相同类型的排队信号,所以我不建议在信号处理程序中使用wait().

Also more than one queued signal of the same type may be handled at once, so I don't recommend using wait() in the signal handler.

这篇关于Python多处理问题/误解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆