Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22002

Re: Best way to disconnect from ldap?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!feed.xsnews.nl!border-1.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'cpython': 0.05; 'method,': 0.05; 'python': 0.08; '21,': 0.09; 'ldap': 0.09; 'method:': 0.09; 'password)': 0.09; 'solution,': 0.09; 'suggestion.': 0.09; 'exception': 0.12; '__del__': 0.16; 'close()': 0.16; 'concur': 0.16; 'finalize': 0.16; 'performed.': 0.16; 'scope,': 0.16; 'skip:\xc2 30': 0.16; 'subject:ldap': 0.16; '\xc2\xa0i': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'wed,': 0.17; 'wrote:': 0.18; 'wrap': 0.18; 'seems': 0.20; 'cheers,': 0.20; "haven't": 0.20; 'cc:no real name:2**0': 0.21; 'wrote': 0.21; 'header:In-Reply-To:1': 0.22; 'timely': 0.25; 'cc:2**0': 0.26; 'code': 0.26; 'bit': 0.28; "i'm": 0.28; 'message- id:@mail.gmail.com': 0.29; 'delay': 0.29; 'explicit': 0.29; 'explicitly': 0.29; 'class': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'closing': 0.30; 'skip:\xc2 20': 0.30; 'chris': 0.30; 'subject:?': 0.31; 'objects': 0.32; 'there': 0.33; 'done.': 0.34; 'done': 0.34; 'url:python': 0.35; 'connection': 0.36; 'received:209.85.214': 0.36; 'question': 0.36; 'before.': 0.37; 'but': 0.37; 'reference': 0.37; 'received:google.com': 0.37; 'similar': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'uses': 0.38; 'could': 0.38; 'some': 0.38; 'url:org': 0.39; 'subject:from': 0.39; 'received:209': 0.39; 'application': 0.40; 'being': 0.40; 'john': 0.61; 'ever': 0.64; 'connection,': 0.73; 'skip:\xc2 10': 0.74; 'connection.': 0.77; '12:30': 0.84; 'about?': 0.84; 'sender:addr:chris': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; 'promptly': 0.93; 'subject:Best': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=XMuV2nZRiWoyStft0QN24D59lyOKiLz2+b5UDCx/w7o=; b=Q3q4YP1bO4MKtFnJCu4gs5dZYIP0J+QWozEEuS3UmgE7M1epjz86BgwIA+pjc13cJb QkDNzL+1rnmrLoYtY6sJU5oJ4dz363oYeJMXP/GHbZyZ835mY/01p1qJypDk4tmSdJQ4 8lRvIQjABUH9xoWJl78weadf2QCj9d3zzbp+A=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding:x-gm-message-state; bh=XMuV2nZRiWoyStft0QN24D59lyOKiLz2+b5UDCx/w7o=; b=H7BfaqDf9jyAGNgwH5CbEjBGt3oN/+ZW1EIqNDXab3KN1kudecmIIiUbA52JxncrFu Z+t816Kr6YypYBuQjslnsmiYG/EtWBjShuzTYbAmjgVi2HCqL817k0FoXsX9DRJ9Unm6 DlNMcqApMZqmyaKroKkgxcyrvuJkVQTx4wnC7dMOZHjvpwH6Y3db5fqac7xGRYjyJ0UI 6brJO5mr6VWjdGp/8ICraTZ11tet1TZmxHgRuK/aMIwmzm4SwT/Reb0ENon43rP6I1bB TFmIgHUJdt/0gCm6pgbAYNkEFhTzJi50fIiHAsuai68XoEafB145ohQLTMXMwM+639oB Qv1A==
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <jkda88$mrk$1@reader1.panix.com>
References <jkda88$mrk$1@reader1.panix.com>
Date Wed, 21 Mar 2012 13:34:29 -0700
X-Google-Sender-Auth nsybPkbsgHiHLGQUqjkxxQB0aFk
Subject Re: Best way to disconnect from ldap?
From Chris Rebert <clp2@rebertia.com>
To John Gordon <gordon@panix.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Gm-Message-State ALoCoQnCIBQ8igK27Re5qkNDXZDo7Bazw1nTFQB85c/+f/pFRIz+ggeYgiTuOEsjNbPNsT8STjAF
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.874.1332362072.3037.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332362072 news.xs4all.nl 6949 [2001:888:2000:d::a6]:35857
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22002

Show key headers only | View raw


On Wed, Mar 21, 2012 at 12:30 PM, John Gordon <gordon@panix.com> wrote:
> I'm writing an application that interacts with ldap, and I'm looking
> for advice on how to handle the connection.  Specifically, how to
> close the ldap connection when the application is done.
>
> I wrote a class to wrap an LDAP connection, similar to this:
>
>    import ldap
>    import ConfigParser
>
>    class MyLDAPWrapper(object):
>
>        def __init__(self):
>
>            config = ConfigParser.SafeConfigParser()
>            config.read('sample.conf')
>
>            uri = config.get('LDAP', 'uri')
>            user = config.get('LDAP', 'user')
>            password = config.get('LDAP', 'password')
>
>            self.ldapClient = ldap.initialize(uri)
>            self.ldapClient.simple_bind_s(user, password)
>
> My question is this: what is the best way to ensure the ldap connection
> gets closed when it should?  I could write an explicit close() method,
> but that seems a bit messy; there would end up being lots of calls to
> close() scattered around in my code (primarily inside exception handlers.)
>
> Or I could write a __del__ method:
>
>        def __del__(self):
>            self.ldapClient.unbind_s()
>
> This seems like a much cleaner solution, as I don't ever have to worry
> about closing the connection; it gets done automatically.

Yes, but not necessarily in a timely manner. Since its uses reference
counting, CPython /just so happens/ to finalize
non-cyclically-referenced objects promptly when they go out of scope,
but Python-the-language makes no such guarantee, and indeed some of
the other Python implementations explicitly disclaim that there may be
a significant delay before finalization is performed.

> I haven't ever used __del__ before.  Are there any 'gotchas' I need to
> worry about?

In addition to the aforementioned problem regarding portability to
other Python implementations, see also the Warning box under:
http://docs.python.org/reference/datamodel.html#object.__del__

I concur with J.'s context manager suggestion.

Cheers,
Chris

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Best way to disconnect from ldap? John Gordon <gordon@panix.com> - 2012-03-21 19:30 +0000
  Re: Best way to disconnect from ldap? "J. Cliff Dyer" <jcd@sdf.lonestar.org> - 2012-03-21 16:21 -0400
  Re: Best way to disconnect from ldap? Chris Rebert <clp2@rebertia.com> - 2012-03-21 13:34 -0700
  Re: Best way to disconnect from ldap? Chris Kaynor <ckaynor@zindagigames.com> - 2012-03-21 13:54 -0700
  Re: Best way to disconnect from ldap? Tycho Andersen <tycho@tycho.ws> - 2012-03-22 08:14 -0500
    Re: Best way to disconnect from ldap? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-22 17:26 +0000
      Re: Best way to disconnect from ldap? Tim Chase <python.list@tim.thechases.com> - 2012-03-22 12:54 -0500
      Re: Best way to disconnect from ldap? Terry Reedy <tjreedy@udel.edu> - 2012-03-22 15:27 -0400
      Re: Best way to disconnect from ldap? Tycho Andersen <tycho@tycho.ws> - 2012-03-22 16:02 -0500
  Re: Best way to disconnect from ldap? Chris Rebert <clp2@rebertia.com> - 2012-03-22 06:27 -0700
  Re: Best way to disconnect from ldap? Tycho Andersen <tycho@tycho.ws> - 2012-03-22 09:00 -0500
  Re: Best way to disconnect from ldap? John Gordon <gordon@panix.com> - 2012-04-05 18:38 +0000
    Re: Best way to disconnect from ldap? Michael Ströder <michael@stroeder.com> - 2012-04-05 20:56 +0200

csiph-web