开发者

Python Event Handler

开发者 https://www.devze.com 2023-03-23 02:19 出处:网络
I am trying to implement an event handler of sort. I am try to collect sample network captures using a external process using Popen and it writes an XML file. I parse the xml file to collect what ever

I am trying to implement an event handler of sort. I am try to collect sample network captures using a external process using Popen and it writes an XML file. I parse the xml file to collect what ever information I require. But I do开发者_JAVA技巧 not want to terminate the process until the number of packets has reached a certain limit.

def getPacketCount(xmlfile, count, pid):
    while 1:
        try:
            parser = minidom.parse(xmlfile)
            wlan = parser.getElementsByTagName('wireless-network')[0]
            pkt = wlan.getElementsByTagName('packets')[1]
            packetCount = pkt.getElementsByTagName('total').childNodes[0].data
            if packetCount>count:
                #Call event handler to kill process with given pid.
        except AttributeError, TypeError:
            print "AttributeError: Accessing file again"`

  • Is there a performance plenty in this approach (Since it's constantly checking the file)?
  • How do make this function runs in the background (like a daemon?)
  • Does it make sense to use Multiprocessing module?
  • It is it efficient to wrap Popen in a function and call Multiprocessing?

    Note: I am implementing this with Django to handle database operations.
    


    django, being a web-framework, is not suited to event driven programming; it's much better suited for the usual request/response cycle of http.

    It may make better sense to make the file-polling program a separate script, (possibly using INotify to avoid the need for frequent polling of the file), and it can then notify the larger web application by making a regular http request to a resource for that purpose, or updating an underlying database the app uses.

  • 0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消