使用Python多处理无法将LDAP对象共享给子进程

时间:2014-09-26 13:10:48

标签: python multiprocessing python-ldap python-multiprocessing

我将有一个子进程来发送LDAP查询和其他子进程来检索响应。 在两个进程之间共享LDAP对象时出现问题。有人能指出我的解决方案吗?

import ldap
from multiprocessing import Process


def send_ldap(ldap_conn):
    print ldap_conn

def receive_ldap(ldap_conn):
    print ldap_conn

def main():
    ldap_conn = ldap.initialize('ldap://abc:12345')
    ldap_sender = Process(target=send_ldap, args=(ldap_conn,))
    ldap_receiver = Process(target=receive_ldap, args=(ldap_conn,))
    ldap_sender.start()


if __name__ == '__main__':
    main()

错误在于pickle模块:

    Traceback (most recent call last):
  File "t.py", line 22, in <module>
    main()
  File "t.py", line 16, in main
    ldap_sender.start()
  File "c:\python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "c:\python27\lib\multiprocessing\forking.py", line 277, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "c:\python27\lib\multiprocessing\forking.py", line 199, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "c:\python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "c:\python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "c:\python27\lib\pickle.py", line 419, in save_reduce
    save(state)
  File "c:\python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "c:\python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "c:\python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 548, in save_tuple
    save(element)
  File "c:\python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "c:\python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "c:\python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "c:\python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "c:\python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "c:\python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "c:\python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
Traceback (most recent call last):
  File "c:\python27\lib\pickle.py", line 396, in save_reduce
  File "<string>", line 1, in <module>
      File "c:\python27\lib\multiprocessing\forking.py", line 381, in main
save(cls)
      File "c:\python27\lib\pickle.py", line 286, in save
self = load(from_parent)
      File "c:\python27\lib\pickle.py", line 1378, in load
f(self, obj) # Call unbound method with explicit self
  File "c:\python27\lib\pickle.py", line 748, in save_global
    return Unpickler(file).load()
  File "c:\python27\lib\pickle.py", line 858, in load
    (obj, module, name))
pickle.PicklingError: Can't pickle <type 'thread.lock'>: it's not found as thread.lock
dispatch[key](self)
  File "c:\python27\lib\pickle.py", line 880, in load_eof
    raise EOFError
EOFError

1 个答案:

答案 0 :(得分:2)

问题是ldap_conn对象不可选,在Windows中的进程之间发送它是必需的。它不可选,因为它在内部使用了一些不可拾取的threading.Lock对象。该库实际上声称提供了一个应该可以选择的ReconnectLDAPObject,但它已被破坏,也不是可挑选的。但是,我们可以通过子类来修复它并修复bug(它错过了在酸洗之前删除其中一个内部锁):

from ldap.ldapobject import ReconnectLDAPObject
from multiprocessing import Process
import ldap

class PicklableLDAPObject(ReconnectLDAPObject):
    def __getstate__(self):
        d = ReconnectLDAPObject.__getstate__(self)
        del d['_reconnect_lock']
        return d

    def __setstate__(self, d):
        self._reconnect_lock = ldap.LDAPLock(desc='reconnect lock within %s' % (repr(self)))
        ReconnectLDAPObject.__setstate__(self, d)

def send_ldap(ldap_conn):
    print ldap_conn

def receive_ldap(ldap_conn):
    print ldap_conn

def main():
    #ldap_conn = ldap.initialize('ldap://abc:12345')
    ldap_conn = PicklableLDAPObject('ldap://abc:12345')
    ldap_sender = Process(target=send_ldap, args=(ldap_conn,))
    ldap_receiver = Process(target=receive_ldap, args=(ldap_conn,))
    ldap_sender.start()

现在这个对象会很好,这意味着它应该可用于multiprocessing目的。