summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorAntoine Pitrou <pitrou@free.fr>2017-03-24 12:52:11 (GMT)
committerGitHub <noreply@github.com>2017-03-24 12:52:11 (GMT)
commit8988945cdc27ffa86ba8c624e095b51c459f5154 (patch)
tree3dd4f0619935cca2a0556f0a9cac10ff0aa6b678 /Lib/test
parente304e33c16e060932d1e2cc8a030d42b02b429b5 (diff)
downloadcpython-8988945cdc27ffa86ba8c624e095b51c459f5154.zip
cpython-8988945cdc27ffa86ba8c624e095b51c459f5154.tar.gz
cpython-8988945cdc27ffa86ba8c624e095b51c459f5154.tar.bz2
bpo-29861: release references to multiprocessing Pool tasks (#743)
* bpo-29861: release references to multiprocessing Pool tasks Release references to tasks, their arguments and their results as soon as they are finished, instead of keeping them alive until another task arrives. * Comments in test
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/_test_multiprocessing.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index b5f4782..1d3bb0f 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -18,6 +18,7 @@ import random
import logging
import struct
import operator
+import weakref
import test.support
import test.support.script_helper
@@ -1738,6 +1739,19 @@ def raise_large_valuerror(wait):
time.sleep(wait)
raise ValueError("x" * 1024**2)
+def identity(x):
+ return x
+
+class CountedObject(object):
+ n_instances = 0
+
+ def __new__(cls):
+ cls.n_instances += 1
+ return object.__new__(cls)
+
+ def __del__(self):
+ type(self).n_instances -= 1
+
class SayWhenError(ValueError): pass
def exception_throwing_generator(total, when):
@@ -1746,6 +1760,7 @@ def exception_throwing_generator(total, when):
raise SayWhenError("Somebody said when")
yield i
+
class _TestPool(BaseTestCase):
@classmethod
@@ -2000,6 +2015,19 @@ class _TestPool(BaseTestCase):
# check that we indeed waited for all jobs
self.assertGreater(time.time() - t_start, 0.9)
+ def test_release_task_refs(self):
+ # Issue #29861: task arguments and results should not be kept
+ # alive after we are done with them.
+ objs = [CountedObject() for i in range(10)]
+ refs = [weakref.ref(o) for o in objs]
+ self.pool.map(identity, objs)
+
+ del objs
+ self.assertEqual(set(wr() for wr in refs), {None})
+ # With a process pool, copies of the objects are returned, check
+ # they were released too.
+ self.assertEqual(CountedObject.n_instances, 0)
+
def raising():
raise KeyError("key")