我将有一个子流程发送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()
泡菜模块出现错误:
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:python27libmultiprocessingprocess.py", line 130, in start
self._popen = Popen(self)
File "c:python27libmultiprocessingforking.py", line 277, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "c:python27libmultiprocessingforking.py", line 199, in dump
ForkingPickler(file, protocol).dump(obj)
File "c:python27libpickle.py", line 224, in dump
self.save(obj)
File "c:python27libpickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "c:python27libpickle.py", line 419, in save_reduce
save(state)
File "c:python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "c:python27libpickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "c:python27libpickle.py", line 681, in _batch_setitems
save(v)
File "c:python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "c:python27libpickle.py", line 548, in save_tuple
save(element)
File "c:python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "c:python27libpickle.py", line 725, in save_inst
save(stuff)
File "c:python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "c:python27libpickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "c:python27libpickle.py", line 681, in _batch_setitems
save(v)
File "c:python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "c:python27libpickle.py", line 725, in save_inst
save(stuff)
File "c:python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "c:python27libpickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "c:python27libpickle.py", line 681, in _batch_setitems
save(v)
File "c:python27libpickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
Traceback (most recent call last):
File "c:python27libpickle.py", line 396, in save_reduce
File "<string>", line 1, in <module>
File "c:python27libmultiprocessingforking.py", line 381, in main
save(cls)
File "c:python27libpickle.py", line 286, in save
self = load(from_parent)
File "c:python27libpickle.py", line 1378, in load
f(self, obj) # Call unbound method with explicit self
File "c:python27libpickle.py", line 748, in save_global
return Unpickler(file).load()
File "c:python27libpickle.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:python27libpickle.py", line 880, in load_eof
raise EOFError
EOFError
问题是ldap_conn
对象不可拾取,这是在Windows中的进程之间发送它所必需的。它不可拾取,因为它在内部使用了一些不可拾取的threading.Lock
对象。该库实际上声称提供了一个ReconnectLDAPObject
,它本应是可拾取的,但它已经损坏,也不可拾取。不过,我们可以通过将其子类化并修复错误(它在酸洗之前错过了删除一个内部锁)来解决这个问题:
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()
现在该对象将pickle fine,这意味着它应该可用于multiprocessing
目的。