python--线程active_count

源码: tests/active_count.py

import time
import unittest
import threading


class TestActiveCount(unittest.TestCase):

    def test_thread_number(self):

        def other_thread():
            time.sleep(10)

        number = 5
        ts = [threading.Thread(target=other_thread, args=())
              for i in range(number)]
        [t.start() for t in ts]
        self.assertEqual(threading.active_count(), number+1)
        time.sleep(15)
        self.assertEqual(threading.active_count(), 1)
        [t.join() for t in ts]

 
 

测试: tests/main.py

import unittest


TEST_MODULE = [
    "ln_threading.tests.active_count"
]


if __name__ == '__main__':
    suite = unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULE)
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite)

你可能感兴趣的:(python--线程active_count)