from concurrent.futures import ThreadPoolExecutor import time import threading import logging import unittest class TestThreadPool(unittest.TestCase): """ In ThreadPoolExecutor's submit method, if the name was not provided then if there's a idle thread or if re-using a thread , it uses their name else: it prefers the thread_name_prefix """ sample = ThreadPoolExecutor(max_workers=2) logging.basicConfig(level=logging.DEBUG) def check(self, a=69, test_with_name=""): logging.info("%s: [%s]", threading.current_thread(), a) time.sleep(1) if test_with_name: self.assertEqual(threading.current_thread().name, test_with_name) def test_default_name_thread(self): # Running this test alone proves that pool uses thread_name_prefix while # creating a thread and if name was not provided self.sample.submit(self.check) self.sample.submit(self.check, args=(66,)) self.sample.submit(self.check, "", 66) def test_name_thread(self): self.sample.submit(self.check, "Test1", 66, "Test1") self.sample.submit(self.check, "Test2", test_with_name="Test2") def test_shutdown(self): self.sample.shutdown() logging.info("Closed") if __name__ == "__main__": unittest.main()