Unable to pass an lxml etree object to a separate process

后端 未结 1 1813
天涯浪人
天涯浪人 2021-01-03 03:22

I\'m working on a project to parse multiple xml files concurrently in python using lxml. When I initialize the process I want my main class to do some work on the XML before

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 03:35

    Use the following code to register simple picklers/unpicklers for lxml Element/ElementTree objects. I used that in the past with lxml and zmq.

    import copy_reg
    try:
        from cStringIO import StringIO
    except ImportError:
        from StringIO import StringIO
    from lxml import etree
    
    def element_unpickler(data):
        return etree.fromstring(data)
    
    def element_pickler(element):
        data = etree.tostring(element)
        return element_unpickler, (data,)
    
    copy_reg.pickle(etree._Element, element_pickler, element_unpickler)
    
    def elementtree_unpickler(data):
        data = StringIO(data)
        return etree.parse(data)
    
    def elementtree_pickler(tree):
        data = StringIO()
        tree.write(data)
        return elementtree_unpickler, (data.getvalue(),)
    
    copy_reg.pickle(etree._ElementTree, elementtree_pickler, elementtree_unpickler)
    

    0 讨论(0)
提交回复
热议问题