问题在于threading.current_thread().name == 'MainThread'一个人总是可以做到:threading.current_thread().name = 'MyName'
assert threading.current_thread().name == 'MainThread' # will fail
也许以下内容更可靠:threading.current_thread().__class__.__name__ == '_MainThread'threading.current_thread().__class__.__name__ = 'Grrrr'
assert threading.current_thread().__class__.__name__ == '_MainThread' # will fail
assert threading.current_thread() == threading.main_thread()
如下面的评论所述,user 1300959的另一个可行和干净的选择是:isinstance(threading.current_thread(), threading._MainThread)