From: Yeargan Y. <ya...@un...> - 2008-01-29 01:04:10
|
I am trying to use the asynchronous LDAP polling feature and have a question about what appears to be a timing issue. When I run the code below, I find that I must insert a sleep() before entering the while loop or I get a ValueError exception. If I sleep for around 0.01 seconds, the exception is intermittent (from my system to this server). With the sleep at 0.10 seconds or higher, I get the results I would expect from the server. I would expect that whether I sleep or not should be irrelevant. If I poll for a result and there is nothing available, then result() should return with None and not generate an exception. Is that the correct behavior? What am I missing? I provided an example of the code output including the exception itself and the sample code below. The code uses a public LDAP server and should work from anywhere. ---=[begin: sample exception]=--- Polling ... Traceback (most recent call last): File "./sample.py", line 26, in ? res = l.result(res_id, all=0, timeout=0) File "/usr/lib/python2.4/site-packages/ldap/ldapobject.py", line 405, in result res_type,res_data,res_msgid = self.result2(msgid,all,timeout) File "/usr/lib/python2.4/site-packages/ldap/ldapobject.py", line 409, in result2 res_type, res_data, res_msgid, srv_ctrls = self.result3 (msgid,all,timeout) File "/usr/lib/python2.4/site-packages/ldap/ldapobject.py", line 415, in result3 rtype, rdata, rmsgid, serverctrls = self._ldap_call (self._l.result3,msgid,all,timeout) ValueError: need more than 3 values to unpack ---=[end: sample exception]=--- ---=[begin: sample code]=--- #!/usr/bin/python import ldap,time l = ldap.initialize('ldap://ldap.utexas.edu') l.simple_bind_s() #anonymous bind base = r'ou=people,dc=directory,dc=utexas,dc=edu' scope = ldap.SCOPE_ONELEVEL filt = r'(&(uid=yw*)(objectclass=inetOrgPerson))' attrs = ['givenname'] res_id = l.search(base, scope, filt, attrs) # exception occurs without this sleep -- WHY?? #time.sleep(0.1) while 1: print print 'Polling ...' # Set all=0 to retrieve a single entry # Set timeout=0 to poll for a response res = l.result(res_id, all=0, timeout=0) if type(res) == tuple: if res[0] == ldap.RES_SEARCH_ENTRY: print 'Got LDAP ENTRY' print 'Data: ', res[1] elif res[0] == ldap.RES_SEARCH_RESULT: print 'Got LDAP RESULT' print 'Data: ', if res[1] == []: print '--empty--' else: print res[1] break #exit while loop else: print '-'*60 print '*** UNEXPECTED LDAP RESULT ***' print 'DEBUG (res): ', res print '-'*60 else: print 'DEBUG: type = ', type(res) print 'DEBUG: value = ', repr(res) continue print l.unbind_s() ---=[end: sample code]=--- Any help appreciated. Yancey |