美文网首页
python--线程active_count

python--线程active_count

作者: 极光火狐狸 | 来源:发表于2018-09-22 16:47 被阅读4次

    源码: 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

          本文链接:https://www.haomeiwen.com/subject/wkdxoftx.html