Skip to content

Commit 8dcf22f

Browse files
authored
bpo-31234: Join threads in test_hashlib (#3573)
* bpo-31234: Join threads in test_hashlib Use thread.join() to wait until the parallel hash tasks complete rather than using events. Calling thread.join() prevent "dangling thread" warnings. * test_hashlib: minor PEP 8 coding style fixes
1 parent 18e95b4 commit 8dcf22f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Lib/test/test_hashlib.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -746,28 +746,28 @@ def test_threaded_hashing(self):
746746
hasher = hashlib.sha1()
747747
num_threads = 5
748748
smallest_data = b'swineflu'
749-
data = smallest_data*200000
749+
data = smallest_data * 200000
750750
expected_hash = hashlib.sha1(data*num_threads).hexdigest()
751751

752-
def hash_in_chunks(chunk_size, event):
752+
def hash_in_chunks(chunk_size):
753753
index = 0
754754
while index < len(data):
755-
hasher.update(data[index:index+chunk_size])
755+
hasher.update(data[index:index + chunk_size])
756756
index += chunk_size
757-
event.set()
758757

759-
events = []
758+
threads = []
760759
for threadnum in range(num_threads):
761-
chunk_size = len(data) // (10**threadnum)
760+
chunk_size = len(data) // (10 ** threadnum)
762761
self.assertGreater(chunk_size, 0)
763762
self.assertEqual(chunk_size % len(smallest_data), 0)
764-
event = threading.Event()
765-
events.append(event)
766-
threading.Thread(target=hash_in_chunks,
767-
args=(chunk_size, event)).start()
768-
769-
for event in events:
770-
event.wait()
763+
thread = threading.Thread(target=hash_in_chunks,
764+
args=(chunk_size,))
765+
threads.append(thread)
766+
767+
for thread in threads:
768+
thread.start()
769+
for thread in threads:
770+
thread.join()
771771

772772
self.assertEqual(expected_hash, hasher.hexdigest())
773773

0 commit comments

Comments
 (0)