test_multiprocessing: Fix dangling process/thread (#2850) · python/cpython@d7e64d9 (original) (raw)
`@@ -32,11 +32,12 @@
`
32
32
`# without thread support.
`
33
33
`import threading
`
34
34
``
35
``
`-
import multiprocessing.dummy
`
36
35
`import multiprocessing.connection
`
37
``
`-
import multiprocessing.managers
`
``
36
`+
import multiprocessing.dummy
`
38
37
`import multiprocessing.heap
`
``
38
`+
import multiprocessing.managers
`
39
39
`import multiprocessing.pool
`
``
40
`+
import multiprocessing.queues
`
40
41
``
41
42
`from multiprocessing import util
`
42
43
``
64
65
`def latin(s):
`
65
66
`return s.encode('latin')
`
66
67
``
``
68
+
``
69
`+
def close_queue(queue):
`
``
70
`+
if isinstance(queue, multiprocessing.queues.Queue):
`
``
71
`+
queue.close()
`
``
72
`+
queue.join_thread()
`
``
73
+
``
74
+
67
75
`#
`
68
76
`# Constants
`
69
77
`#
`
`@@ -825,6 +833,7 @@ def test_qsize(self):
`
825
833
`self.assertEqual(q.qsize(), 1)
`
826
834
`q.get()
`
827
835
`self.assertEqual(q.qsize(), 0)
`
``
836
`+
close_queue(q)
`
828
837
``
829
838
`@classmethod
`
830
839
`def _test_task_done(cls, q):
`
`@@ -897,6 +906,7 @@ def reduce(self):
`
897
906
`q.put(True)
`
898
907
`# bpo-30595: use a timeout of 1 second for slow buildbots
`
899
908
`self.assertTrue(q.get(timeout=1.0))
`
``
909
`+
close_queue(q)
`
900
910
``
901
911
`#
`
902
912
`#
`
`@@ -1020,10 +1030,12 @@ def test_notify(self):
`
1020
1030
`p = self.Process(target=self.f, args=(cond, sleeping, woken))
`
1021
1031
`p.daemon = True
`
1022
1032
`p.start()
`
``
1033
`+
self.addCleanup(p.join)
`
1023
1034
``
1024
1035
`p = threading.Thread(target=self.f, args=(cond, sleeping, woken))
`
1025
1036
`p.daemon = True
`
1026
1037
`p.start()
`
``
1038
`+
self.addCleanup(p.join)
`
1027
1039
``
1028
1040
`# wait for both children to start sleeping
`
1029
1041
`sleeping.acquire()
`
`@@ -1066,11 +1078,13 @@ def test_notify_all(self):
`
1066
1078
`args=(cond, sleeping, woken, TIMEOUT1))
`
1067
1079
`p.daemon = True
`
1068
1080
`p.start()
`
``
1081
`+
self.addCleanup(p.join)
`
1069
1082
``
1070
1083
`t = threading.Thread(target=self.f,
`
1071
1084
`args=(cond, sleeping, woken, TIMEOUT1))
`
1072
1085
`t.daemon = True
`
1073
1086
`t.start()
`
``
1087
`+
self.addCleanup(t.join)
`
1074
1088
``
1075
1089
`# wait for them all to sleep
`
1076
1090
`for i in range(6):
`
`@@ -1089,10 +1103,12 @@ def test_notify_all(self):
`
1089
1103
`p = self.Process(target=self.f, args=(cond, sleeping, woken))
`
1090
1104
`p.daemon = True
`
1091
1105
`p.start()
`
``
1106
`+
self.addCleanup(p.join)
`
1092
1107
``
1093
1108
`t = threading.Thread(target=self.f, args=(cond, sleeping, woken))
`
1094
1109
`t.daemon = True
`
1095
1110
`t.start()
`
``
1111
`+
self.addCleanup(t.join)
`
1096
1112
``
1097
1113
`# wait for them to all sleep
`
1098
1114
`for i in range(6):
`
`@@ -1123,10 +1139,12 @@ def test_notify_n(self):
`
1123
1139
`p = self.Process(target=self.f, args=(cond, sleeping, woken))
`
1124
1140
`p.daemon = True
`
1125
1141
`p.start()
`
``
1142
`+
self.addCleanup(p.join)
`
1126
1143
``
1127
1144
`t = threading.Thread(target=self.f, args=(cond, sleeping, woken))
`
1128
1145
`t.daemon = True
`
1129
1146
`t.start()
`
``
1147
`+
self.addCleanup(t.join)
`
1130
1148
``
1131
1149
`# wait for them to all sleep
`
1132
1150
`for i in range(6):
`
`@@ -1309,6 +1327,7 @@ def test_event(self):
`
1309
1327
`p.daemon = True
`
1310
1328
`p.start()
`
1311
1329
`self.assertEqual(wait(), True)
`
``
1330
`+
p.join()
`
1312
1331
``
1313
1332
`#
`
1314
1333
`# Tests for Barrier - adapted from tests in test/lock_tests.py
`
`@@ -1654,6 +1673,7 @@ def test_thousand(self):
`
1654
1673
`p = self.Process(target=self._test_thousand_f,
`
1655
1674
`args=(self.barrier, passes, child_conn, lock))
`
1656
1675
`p.start()
`
``
1676
`+
self.addCleanup(p.join)
`
1657
1677
``
1658
1678
`for i in range(passes):
`
1659
1679
`for j in range(self.N):
`