【问题标题】:Two simultaneous Python loops with one result两个同时的 Python 循环和一个结果
【发布时间】:2011-09-28 20:58:30
【问题描述】:

我目前有一段 Python 2.6 代码同时运行两个循环。该代码使用 gps (gpsd) 模块和 scapy 模块。基本上,第一个函数 (gpsInfo) 包含一个连续的 while 循环,从 GPS 设备抓取 GPS 数据并将位置写入控制台。第二个函数(ClientDetect)在一个连续循环中运行,它还会嗅探空气中的 wifi 数据,并在找到特定数据包时打印此数据。我已经将这两个循环与作为后台线程运行的 GPS 循环连接起来。我想要做的(并且一直在努力解决 5 天的问题)是因为,当 ClientDetect 函数找到匹配项并打印相应的信息时,我希望该命中时的相应 GPS 坐标也打印到安慰。目前我的代码似乎不起作用。

observedclients = [] p = ""  # Relate to wifi packet session =
gps.gps(mode=gps.WATCH_NEWSTYLE)

def gpsInfo():

    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            print session.fix.latitude + session.fix.longitude
            time.sleep(0.1)

def WifiDetect(p):
    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                print p.addr2
                observedclients.append(p.addr2)  

def ActivateWifiDetect():
    sniff(iface="mon0", prn=WifiDetect)

if __name__ == '__main__':
    t = threading.Thread(target=gpsInfo)
    t.start()
    WifiDetect()

任何人都可以查看我的代码,看看如何最好地同时获取数据,以便在 wifi 命中时同时打印 GPS 坐标?有人提到实现排队,但我对此进行了研究,但对于如何实现它无济于事。

如前所述,此代码的目的是扫描 GPS 和特定 wifi 数据包,并在检测到时打印与数据包相关的详细信息以及检测到的位置。

【问题讨论】:

    标签: python gps queue python-multithreading gpsd


    【解决方案1】:

    一个简单的方法是将gps位置存储在一个全局变量中,并让wifi嗅探线程在需要打印一些数据时读取该全局变量;问题在于,由于两个线程可以同时访问全局变量,因此您需要使用互斥锁来包装它;

    last_location = (None, None)
    location_mutex = threading.Lock()
    
    def gpsInfo():
        global last_location
        while True:
            session.poll()
            time.sleep(5)
            if gps.PACKET_SET:
                session.stream
                with location_mutex:
                    # DON'T Print from inside thread!
                    last_location = session.fix.latitude, session.fix.longitude
                time.sleep(0.1)
    
    def WifiDetect(p):
        if p.haslayer(Dot11):
            if p.type == 0 and p.subtype in stamgmtstypes:
                if p.addr2 not in observedclients:
                    with location_mutex:
                        print p.addr2, last_location
                        observedclients.append((p.addr2, last_location))  
    

    【讨论】:

    • 虽然有了互斥锁/锁,我不会停止 GPS 循环等待检测到 wifi 数据包吗?我的意思是,在嗅探特定的 wifi 数据包之前可能需要 5 分钟,如果是这样,由于检测到的第一个 GPS 位置在等待 wifi 结果时被锁定,GPS 位置不会过时吗?
    • 不,锁只保留足够长的时间来读取或写入该变量,两个线程都不会在持有锁的同时等待 IO。实际阻塞该锁的可能性非常低。您可能会看到这实际等待锁定的唯一时间是 print 实际将换行符刷新到控制台所需的时间,这不是很长时间。
    • 感谢 TokenMacGuy。我实际上已经修改了我的代码以从 GPS 设备中获取 Unix 时间,只是为了与检测到数据包的时间进行比较,并且它们完全同步到秒,这反映了报告的位置确实是数据包时的位置闻了闻。
    【解决方案2】:

    当你在函数中使用 gps 时,你需要告诉 python 你正在使用一个外部变量。代码应如下所示:

    def gpsInfo():
        global gps # new line
    
        while True:
            session.poll()
            time.sleep(5)
            if gps.PACKET_SET:
                session.stream
                print session.fix.latitude + session.fix.longitude
                time.sleep(0.1)
    
    
    def WifiDetect(p):
        global p, observedclients # new line
    
        if p.haslayer(Dot11):
            if p.type == 0 and p.subtype in stamgmtstypes:
                if p.addr2 not in observedclients:
                    print p.addr2
                    observedclients.append(p.addr2)  
    

    【讨论】:

    • 通常不需要指定全局。仅当您想重新绑定它时。
    • 嗯我一直有很多问题没有指定全局。
    【解决方案3】:

    我认为你的目标应该更具体。

    如果您只想在嗅探 Wifi 网络时获取 GPS 坐标,只需执行(伪代码):

    while True:
        if networkSniffed():
            async_GetGPSCoords()
    

    如果您想要记录所有 GPS 坐标并希望将其与 Wifi 网络数据匹配,只需将所有数据连同时间戳一起打印出来,然后进行后处理,通过时间戳将 Wifi 网络与 GPS 坐标匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多