[lxml-dev] Error with thread
Stefan Behnel
stefan_ml at behnel.de
Tue Oct 30 13:36:23 CET 2007
Frédéric Mantegazza wrote:
> Le lundi 29 octobre 2007 17:36, Stefan Behnel a écrit :
> [1.1.1]
>> That's a pretty old version, but the general restriction still applies.
>> You cannot use an XSLT object in a different thread if it was not created
>> in the main thread (that's due to some optimisations in libxslt).
>
> Before using lxml, I was using lxml2, which is also based on libxml2 and
> libxslt, and I had not this thread problem.
I assume you meant "libxml2", the Python wrapper around the libxml2 C library.
> Is this optimisation in your
> C/python code, or in the original C libxslt code?
We are talking about two optimisations here, one in libxslt and one in lxml.
libxslt uses a hash table for XML names to avoid re-allocation of memory.
AFAIR, the optimisation is that the dict used for the generated document
inherits from the dict of the stylesheet document, which is treated as
read-only dictionary fallback.
The optimisation in lxml is that it uses one dictionary per thread, for all
documents that are parsed in that thread. So, if you take a stylesheet that
was parsed in one thread (and thus depends on the dictionary of that thread),
and use it in a different thread that uses a different dictionary, you end up
with a result document that tries to mix entries from different dictionaries
and will therefore free some of them although they are still referenced in the
dictionary of another thread. A sure way to crash your system.
Sadly, the dictionary used by libxslt is not configurable, so all we can do is
raise an exception if we detect this problem. I don't really see that as a
disadvantage, as it is a fast and safe solution, and you can usually work
around the restriction without major hassle.
Stefan
More information about the lxml-dev
mailing list