Re: [lxml-dev] segfault from thread on Windows

Hi Scott, Scott Haeger wrote:
Here is a small code snippet that demostrates my problem.
As you mentioned, I can't reproduce any problems with this under Linux. Couldn't test it under Windows.
The code works just fine as it is written. However, python crashes if you comment out the parseFile( ) call from the main thread.
I assume that what happens here is that libxml2 misses its thread-local setup.
This seems to be contrary to what you described about using the default parser from different threads.
def parseFile(): s = "<test><a>entrya</a><b>entryb</b></test>" sio = StringIO.StringIO (s) tree = etree.parse(sio) tree.write(sys.stdout) sys.stdout.flush()
parseFile() t = thread.start_new_thread(parseFile, ())
I understand what you did now. The current way do it this would be something like: def parseFileFromThread(): etree.initThread() # needs the current trunk to work thread_local_parser = etree.XMLParser() s = "<test><a>entrya</a><b>entryb</b></test>" sio = StringIO.StringIO(s) tree = etree.parse(sio, thread_local_parser) tree.write(sys.stdout) parseFile() t1 = thread.start_new_thread(parseFileFromThread, ()) t2 = thread.start_new_thread(parseFileFromThread, ()) 1.0.beta did not have an initThread() function yet (it had an initThreadLogging function, which did part of what the new initThread does). However, I noticed that it is needed to set up some thread-local libxml2 options. This means that you have to call this method from a newly created thread. It doesn't do any harm to call it from the main thread, though, so you can call the above parseFileFromThread also from the main thread. So, hopefully, your problem is solved in the current trunk. I'd be glad if you could test that with the above modifications to your code. I guess I'll have to write up some documentation on this, but it's good to have some feedback first. Stefan
participants (1)
-
Stefan Behnel