diff --git a/Lib/multiprocessing/__init__.py b/Lib/multiprocessing/__init__.py index 0031a5e94ff..bbdad809dc9 100644 --- a/Lib/multiprocessing/__init__.py +++ b/Lib/multiprocessing/__init__.py @@ -116,7 +116,8 @@ def cpu_count(): num = 0 elif 'bsd' in sys.platform or sys.platform == 'darwin': try: - num = int(os.popen('sysctl -n hw.ncpu').read()) + with os.popen('sysctl -n hw.ncpu') as p: + num = int(p.read()) except ValueError: num = 0 else: diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index 693026227fd..ac02d5ab250 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -1259,7 +1259,11 @@ def test_rapid_restart(self): authkey = os.urandom(32) manager = QueueManager( address=('localhost', 0), authkey=authkey, serializer=SERIALIZER) - addr = manager.get_server().address + srvr = manager.get_server() + addr = srvr.address + # Close the connection.Listener socket which gets opened as a part + # of manager.get_server(). It's not needed for the test. + srvr.listener.close() manager.start() p = self.Process(target=self._putter, args=(manager.address, authkey))