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"`
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.
精彩评论