Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22003
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ckaynor@zindagigames.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'help?': 0.03; 'context': 0.04; 'cpython': 0.05; 'method,': 0.05; 'decent': 0.07; 'python': 0.08; '21,': 0.09; 'called.': 0.09; 'cleaned': 0.09; 'garbage': 0.09; 'ldap': 0.09; 'method:': 0.09; 'password)': 0.09; 'solution,': 0.09; 'suggestion.': 0.09; 'throw': 0.09; 'exception': 0.12; 'def': 0.13; 'useful,': 0.13; 'forgotten': 0.15; '__del__': 0.16; 'close()': 0.16; 'concur': 0.16; 'f()': 0.16; 'finalize': 0.16; 'gc.collect()': 0.16; 'issue:': 0.16; 'methods,': 0.16; 'performed.': 0.16; 'runtimeerror': 0.16; 'scope,': 0.16; 'subject:ldap': 0.16; 'valueerror': 0.16; '\xa0def': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'wed,': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'exists': 0.18; 'wrap': 0.18; 'seems': 0.20; 'cheers,': 0.20; "haven't": 0.20; 'memory': 0.21; '(most': 0.21; 'cc:no real name:2**0': 0.21; 'wrote': 0.21; 'holds': 0.21; 'pointed': 0.21; 'maybe': 0.21; 'header:In-Reply- To:1': 0.22; 'interface': 0.23; 'personally,': 0.23; 'traceback': 0.24; 'timely': 0.25; 'code': 0.26; 'up.': 0.26; 'url:mailman': 0.27; 'import': 0.27; 'raise': 0.28; 'separate': 0.28; 'bit': 0.28; "i'm": 0.28; 'message-id:@mail.gmail.com': 0.29; 'delay': 0.29; 'explicit': 0.29; 'explicitly': 0.29; 'onto': 0.29; 'class': 0.29; 'example': 0.29; 'problem': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; '(and': 0.30; 'closing': 0.30; 'generally': 0.30; 'context,': 0.30; 'cycles': 0.30; 'object.': 0.30; 'chris': 0.30; 'subject:?': 0.31; 'url:listinfo': 0.32; 'cases': 0.32; 'skip:\xa0 30': 0.32; 'yet': 0.32; 'break': 0.32; 'objects': 0.32; 'there': 0.33; 'received:209.85.212': 0.33; 'object': 0.33; 'file': 0.34; 'done.': 0.34; 'done': 0.34; '8bit%:3': 0.34; 'last):': 0.34; 'url:python': 0.35; 'connection': 0.36; 'question': 0.36; 'cc:2**1': 0.36; 'before.': 0.37; 'but': 0.37; 'reference': 0.37; 'received:google.com': 0.37; 'similar': 0.37; 'another': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'uses': 0.38; 'cases,': 0.38; 'references': 0.38; 'could': 0.38; 'some': 0.38; 'should': 0.38; 'fail': 0.39; 'possible,': 0.39; 'skip:\xa0 10': 0.39; 'url:org': 0.39; 'option': 0.39; 'subject:from': 0.39; 'received:209': 0.39; 'application': 0.40; 'being': 0.40; 'user': 0.40; 'john': 0.61; 'quick': 0.61; 'more': 0.61; 'your': 0.61; 'full': 0.62; 'managers': 0.62; '8bit%:4': 0.63; 'manager.': 0.64; 'ever': 0.64; 'guarantee': 0.66; 'collection': 0.69; 'connection,': 0.73; 'connection.': 0.77; '12:30': 0.84; 'about?': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; 'immediately,': 0.91; 'received:209.85.212.178': 0.91; 'promptly': 0.93; 'subject:Best': 0.93; 'anymore,': 0.95 |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding:x-gm-message-state; bh=4M74q9ODOgBIGT3lmKTuI9KlJC9xxhVkpk8vi7ao8AE=; b=YfsPOljvgiD2dpSUur6Lwt4vXcNxFPLTO9FR/zANXrTveoDzUYT50rgTZpwVDjeJp0 RNObGmX2y5oR/pnuBRwcbaErDA6obAhaHS9OOLNjT8dwwA7W98GaAV9lu4TEAa3DuWGx ZtozfQl+2AKWc3S7F0QBJ+wGDWiDz9neWJo9dC0IaOfubC24x/BEsYnry4BL4sOL6st0 noSZUuqNMBOhpZaxiGT/Sh6rJLhJtC4at1ZatblgTue5GszDTX2Br431lPiGAo6N64vh xbs6VtLvwVJN0vQcb5SLK6jgwnacOZfnMXJEkjYXFSrOrDH9ojUdX9lIA5wuiLqQ+Ps2 mRYQ== |
| MIME-Version | 1.0 |
| In-Reply-To | <CAMZYqRQR0VFweQgN8jb3wkx-opabvVQJy5zRtQQrv7tnnzivwA@mail.gmail.com> |
| References | <jkda88$mrk$1@reader1.panix.com> <CAMZYqRQR0VFweQgN8jb3wkx-opabvVQJy5zRtQQrv7tnnzivwA@mail.gmail.com> |
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Date | Wed, 21 Mar 2012 13:54:13 -0700 |
| Subject | Re: Best way to disconnect from ldap? |
| To | Chris Rebert <clp2@rebertia.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| X-Gm-Message-State | ALoCoQnl/TYXQBwEPZzpjtCWLPhbqOBcvjZVO/gwPs60nfeqVmpuYOkL2joEUD0Ei6a+MOfQl00k |
| Cc | python-list@python.org, John Gordon <gordon@panix.com> |
| 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.875.1332363276.3037.python-list@python.org> (permalink) |
| Lines | 128 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1332363276 news.xs4all.nl 6889 [2001:888:2000:d::a6]:46775 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:22003 |
Show key headers only | View raw
On Wed, Mar 21, 2012 at 1:34 PM, Chris Rebert <clp2@rebertia.com> wrote:
>
> 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.
Personally, I would combine both methods (and maybe throw in a close
option as well). The standard interface would be to use the with
context, however in cases where that is not possible, an explicit
close is useful, and just in-case that is forgotten or missed, the
__del__ is there as a final backup.
The main case that context managers fail is when you need to break the
creation and teardown into separate methods, such as when writing a
more complex context manager.
As Chris Rebert pointed out, there is no guarantee as to when the
__del__ method is called. CPython will generally call it immediately,
however if there are reference cycles it may never call it:
class O(object):
def __del__(self):
print 'del'
a = O()
b = O()
a.obj = b
b.obj = a
del a
del b # After this, all references should be gone. Netiher a nor b are
accessable anymore, right?
# Yet del was never printed. Maybe a full garbage collection will help?
import gc
gc.collect()
# Nope...
Also, if the object exists and an exception is thrown, the object may
be held onto for extended periods of time, or may never get cleaned
up. A quick example of this issue:
>>> class O(object):
... def __del__(self):
... print 'del'
...
>>> def F():
... o = O()
... raise RuntimeError()
...
>>> F() # o is not garbage collected as sys.exc_info holds a reference to it still in the traceback object.
RuntimeError
Traceback (most recent call last):
File "<stdin-inspect>", line 1, in <module>
File "<stdin-inspect>", line 3, in F
RuntimeError
>>> raise ValueError() # When another exception got thrown, it will get cleaned up....
del
ValueError
Traceback (most recent call last):
File "<stdin-inspect>", line 1, in <module>
ValueError
In any case, it still makes a decent fall-back in case the user of
your code fails to properly clean-up. It will cover many of the common
cases, though you do need to be careful to never get into a reference
cycle if you have __del__ methods, or you get memory leaks.
>
>
> Cheers,
> Chris
> --
> http://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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