I have an XML document coming in over a socket that I need to parse and react to on the fly (ie parsing a partial tree). What I'd like is a non blocking method of doing so, so that I can do other things while waiting for more data to come in (without threading).
Something like iterparse would be ideal if it finished iterating when the read buffer was empty, eg:
context = iterparse(imaginary_socket_file_wrapper)
while 1:
for event, elem in context:
process_elem(elem)
# iteration of context finishes when socket has no more data
do_other_stuff()
time.sleep(0.1)
I guess SAX would also be an option, but iterparse just seems simpler for my needs. Any ideas?
Update:
Using threads is fine, but introduces a level of complexity that I was hoping to sidestep. I thought that non-blocking calls would be a good way to do so, but I'm finding that it increases the complexity of parsing the XML.