美文网首页
python--线程current_thread

python--线程current_thread

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

    源码: tests/current_thread.py

    # -.- coding:utf-8 -.-
    import unittest
    import threading
    
    
    class TestCurrentThread(unittest.TestCase):
    
        def test_main_thread(self):
            current_thread = threading.current_thread()
            self.assertEqual(current_thread.name, "MainThread")
    
        def test_other_thread(self):
    
            result = []
    
            def other_thread():
                current_thread = threading.current_thread()
                result.append(current_thread)
    
            t = threading.Thread(target=other_thread, args=())
            t.start()
            t.join()
    
            self.assertTrue(result[0].name.startswith("Thread-"))
            return result[0]
    
        def test_main_thread_is_not_other_thread(self):
            current_thread = threading.current_thread()
            other_thread = self.test_other_thread()
            self.assertEqual(current_thread.name, "MainThread")
            self.assertTrue(other_thread.name.startswith("Thread-"))
            self.assertNotEqual(current_thread.ident, other_thread.ident)
    
    

     
     

    运行: tests/main.py

    import unittest
    
    
    TEST_MODULE = [
        "ln_threading.tests.current_thread",
    ]
    
    
    if __name__ == '__main__':
        suite = unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULE)
        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)
    
    

    相关文章

      网友评论

          本文标题:python--线程current_thread

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