为什么在更改完成之前线程被中断

时间:2010-10-10 19:09:16

标签: python multithreading

我正在尝试创建用于通过IP地址获取MAC地址的python模块。

def getMACs(addressesList):
    def _processArp(pkt):
        spa = _inet_ntoa(pkt.spa) 
        if pkt.op == dpkt.arp.ARP_OP_REPLY and spa in _cache.macTable:
            lock.acquire()
            try:
                _cache.macTable[spa] = _packedToMacStr(pkt.sha)
                _cache.notFilledMacs -= 1
            finally:
                lock.release()
            if _cache.notFilledMacs == 0:
                thrd.stop()

    addresses = _parseAddresses(addressesList)

    _cache.registerCacheEntry("macTable", {})
    _cache.registerCacheEntry("notFilledMacs", 0)
    _events.arpPacket += _processArp

    lock = threading.Lock()

    thrd = _CaptureThread(promisc=False, timeout_ms=30, filter="arp")
    thrd.start()
    for addr in addresses:
        if _sendArpQuery(addr):
            _cache.macTable[str(addr)] = None
            _cache.notFilledMacs += 1
    thrd.join(125)
    thrd.stop()
    return _cache.macTable

if __name__ == "__main__":
    macTable = getMACs([IPAddress("192.168.1.1"), IPAddress("192.168.1.3")])
    _pprint.pprint(macTable)

当我运行这个模块时,我得到了

{'192.168.1.1': '00:11:95:9E:25:B1', '192.168.1.3': None}

当我逐步调试_processArp时,我得到了

{'192.168.1.1': '00:11:95:9E:25:B1', '192.168.1.3': '00:21:63:78:98:8E'}

Class CaptureThread:

class CaptureThread(threading.Thread):
    def __init__ (self, name=None, snaplen=65535, promisc=True, timeout_ms=0, immediate=False, filter=None):
        threading.Thread.__init__(self)
        self.__running = True
        self.__name = name 
        self.__snaplen = snaplen 
        self.__promisc = promisc 
        self.__timeout_ms = timeout_ms 
        self.__immediate = immediate 
        self.__filter = filter 

    def stop(self):
        self.__running = False

    def run(self):
        self.__pc = pcap.pcap(self.__name, self.__snaplen, self.__promisc, self.__timeout_ms, self.__immediate)
        if self.__filter:
            self.__pc.setfilter(self.__filter)

        while self.__running:
            self.__pc.dispatch(1, self.__processPacket)

    def __processPacket(self, timestamp, pkt):
        peth = dpkt.ethernet.Ethernet(pkt)
        if isinstance(peth.data, dpkt.arp.ARP):
            _events.arpPacket(peth.data)

1 个答案:

答案 0 :(得分:0)

愚蠢的错误。与使用线程时一样 - 因为线程同步。

我打断线程的一个条件是“_cache.notFilledMacs == 0”。在主线程中_cache.notFilledMacs在CaptureThread值减少时没有时间获取值2。

相关问题