[python-ldap] Processing query in spans of time with timeout and resuming

William Brown william at blackhats.net.au
Mon Jul 4 21:08:47 EDT 2022



> On 2 Jul 2022, at 21:01, Max Gautier <mg at max.gautier.name> wrote:
> 
> Hi everyone,
> 
> I'm trying to use python-ldap to write a vim completion plugin
> (more specically, a completion source for vim-asyncomplete[1]), and I'm not sure if
> what I want is possible. 
> 
> Basically, I'd like to do launch a LDAP query, and return results either:
> 1. When the query is finished
> 2. After a soft timeout
> With the possibility in the second case to re-loop and each time get the
> results up to now. 
> 
> I was trying to use the ldap.resiter.ResultProcessor as the docs seems
> to recommend that, but I think I would need to wrap it in something else
> to obtain the desired behaviour.
> 
> I thought about doing it this way:
> 
> ---
> class LdapSoftTimeout(ldap.ldapobject.LDAPObject, ldap.resiter.ResultProcessor):
>    pass
> 
> con = ldap.initialize("ldaps://myuri")
> con.sasl_gssapi_bind_s()
> msg_id = con.search('ou=users,dc=mycompany,dc=com', ldap.SCOPE_SUBTREE, '(cn=*Go*)', ['cn', 'mail'])
> 
> # Keep that whole context somehow, then do a function returning partial
> # results each time it's called, something like :
> 
> partial_results = list()
> now = time.time()
> timeout = 0.1
> for r in con.allresults(msgid=search):
>    if time.time() > now + timeout
>        break
>    list.append(some_processing(r))
> 
> return partial_results
> ---
> 
> However this does not seem very efficient to me.
> Does someone have encountered that kind of usecase and would have a
> better idea ?
> I'm not a very experienced pythonista, so I might have missed something
> obvious, but my search on google on this mailing list were not fruitful,
> so don't hesitate to point me to the obvious solution if there is one.

You can create a thread that does the con.results() call, and then queue the results in memory for the main thread to dequeue and read from. I think thatd be your best bet. 

> 
> Thanks a lot :)
> 
> -- 
> Max Gautier
> _______________________________________________
> python-ldap mailing list
> python-ldap at python.org
> https://mail.python.org/mailman/listinfo/python-ldap

--
Sincerely,

William



More information about the python-ldap mailing list