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: 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: References: 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 To: John Gordon 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Mar 21, 2012 at 12:30 PM, John Gordon wrote: > I'm writing an application that interacts with ldap, and I'm looking > for advice on how to handle the connection. =C2=A0Specifically, how to > close the ldap connection when the application is done. > > I wrote a class to wrap an LDAP connection, similar to this: > > =C2=A0 =C2=A0import ldap > =C2=A0 =C2=A0import ConfigParser > > =C2=A0 =C2=A0class MyLDAPWrapper(object): > > =C2=A0 =C2=A0 =C2=A0 =C2=A0def __init__(self): > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0config =3D ConfigParser.SafeConf= igParser() > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0config.read('sample.conf') > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0uri =3D config.get('LDAP', 'uri'= ) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0user =3D config.get('LDAP', 'use= r') > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0password =3D config.get('LDAP', = 'password') > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.ldapClient =3D ldap.initial= ize(uri) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.ldapClient.simple_bind_s(us= er, password) > > My question is this: what is the best way to ensure the ldap connection > gets closed when it should? =C2=A0I could write an explicit close() metho= d, > 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: > > =C2=A0 =C2=A0 =C2=A0 =C2=A0def __del__(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.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. =C2=A0Are 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