diff options
author | Richard Oudkerk <shibturn@gmail.com> | 2012-06-18 16:47:52 (GMT) |
---|---|---|
committer | Richard Oudkerk <shibturn@gmail.com> | 2012-06-18 16:47:52 (GMT) |
commit | d69cfe88eae5b177b1aaf51c39e85fb92c34cf22 (patch) | |
tree | 43151241e2aab605f65e3634affd630ea6e084a3 /Lib/multiprocessing/connection.py | |
parent | 0f884273b0319272ca32d529af5c761d6dfa8a34 (diff) | |
download | cpython-d69cfe88eae5b177b1aaf51c39e85fb92c34cf22.zip cpython-d69cfe88eae5b177b1aaf51c39e85fb92c34cf22.tar.gz cpython-d69cfe88eae5b177b1aaf51c39e85fb92c34cf22.tar.bz2 |
Issue #15064: Implement context manager protocol for multiprocessing types
Diffstat (limited to 'Lib/multiprocessing/connection.py')
-rw-r--r-- | Lib/multiprocessing/connection.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py index 56f375d..e5694e3 100644 --- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -257,6 +257,12 @@ class _ConnectionBase: self._check_readable() return self._poll(timeout) + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_tb): + self.close() + if _winapi: @@ -436,6 +442,8 @@ class Listener(object): Returns a `Connection` object. ''' + if self._listener is None: + raise IOError('listener is closed') c = self._listener.accept() if self._authkey: deliver_challenge(c, self._authkey) @@ -446,11 +454,19 @@ class Listener(object): ''' Close the bound socket or named pipe of `self`. ''' - return self._listener.close() + if self._listener is not None: + self._listener.close() + self._listener = None address = property(lambda self: self._listener._address) last_accepted = property(lambda self: self._listener._last_accepted) + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_tb): + self.close() + def Client(address, family=None, authkey=None): ''' |