diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2009-07-17 12:07:01 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2009-07-17 12:07:01 (GMT) |
commit | c57a84f41a5d7763d1cd0b648d10479d9c0c6d08 (patch) | |
tree | 75b6dbc2fdd5603dba0ebb981a6ceca7f9037845 /Lib | |
parent | 260484d12aae264ac524f8c9285bec2cf43fa44e (diff) | |
download | cpython-c57a84f41a5d7763d1cd0b648d10479d9c0c6d08.zip cpython-c57a84f41a5d7763d1cd0b648d10479d9c0c6d08.tar.gz cpython-c57a84f41a5d7763d1cd0b648d10479d9c0c6d08.tar.bz2 |
Merged revisions 73694,73708,73738 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73694 | jesse.noller | 2009-06-29 14:24:26 -0400 (Mon, 29 Jun 2009) | 1 line
Issue 5740: multiprocessing.connection.* authkey fixes
........
r73708 | jesse.noller | 2009-06-30 13:11:52 -0400 (Tue, 30 Jun 2009) | 1 line
Resolves issues 5155, 5313, 5331 - bad file descriptor error with processes in processes
........
r73738 | r.david.murray | 2009-06-30 22:49:10 -0400 (Tue, 30 Jun 2009) | 2 lines
Make punctuation prettier and break up run-on sentence.
........
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/multiprocessing/process.py | 3 | ||||
-rw-r--r-- | Lib/test/test_multiprocessing.py | 70 |
2 files changed, 71 insertions, 2 deletions
diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index 4a06a45..1a5c25f 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -221,7 +221,8 @@ class Process(object): self._counter = itertools.count(1) if sys.stdin is not None: try: - os.close(sys.stdin.fileno()) + sys.stdin.close() + sys.stdin = open(os.devnull) except (OSError, ValueError): pass _current_process = self diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index 2189ff5..18066ba 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -8,6 +8,7 @@ import unittest import threading import queue as pyqueue import time +import io import sys import os import gc @@ -1868,7 +1869,74 @@ class TestInitializers(unittest.TestCase): p.join() self.assertEqual(self.ns.test, 1) -testcases_other = [OtherTest, TestInvalidHandle, TestInitializers] +# +# Issue 5155, 5313, 5331: Test process in processes +# Verifies os.close(sys.stdin.fileno) vs. sys.stdin.close() behavior +# + +def _ThisSubProcess(q): + try: + item = q.get(block=False) + except pyqueue.Empty: + pass + +def _TestProcess(q): + queue = multiprocessing.Queue() + subProc = multiprocessing.Process(target=_ThisSubProcess, args=(queue,)) + subProc.start() + subProc.join() + +def _afunc(x): + return x*x + +def pool_in_process(): + pool = multiprocessing.Pool(processes=4) + x = pool.map(_afunc, [1, 2, 3, 4, 5, 6, 7]) + +class _file_like(object): + def __init__(self, delegate): + self._delegate = delegate + self._pid = None + + @property + def cache(self): + pid = os.getpid() + # There are no race conditions since fork keeps only the running thread + if pid != self._pid: + self._pid = pid + self._cache = [] + return self._cache + + def write(self, data): + self.cache.append(data) + + def flush(self): + self._delegate.write(''.join(self.cache)) + self._cache = [] + +class TestStdinBadfiledescriptor(unittest.TestCase): + + def test_queue_in_process(self): + queue = multiprocessing.Queue() + proc = multiprocessing.Process(target=_TestProcess, args=(queue,)) + proc.start() + proc.join() + + def test_pool_in_process(self): + p = multiprocessing.Process(target=pool_in_process) + p.start() + p.join() + + def test_flushing(self): + sio = io.StringIO() + flike = _file_like(sio) + flike.write('foo') + proc = multiprocessing.Process(target=lambda: flike.flush()) + flike.flush() + assert sio.getvalue() == 'foo' + +testcases_other = [OtherTest, TestInvalidHandle, TestInitializers, + TestStdinBadfiledescriptor] # # |