[issue36084] Threading: add builtin TID attribute to Thread objects

STINNER Victor report at bugs.python.org
Mon May 13 05:17:18 EDT 2019


STINNER Victor <vstinner at redhat.com> added the comment:

> """Native integral thread ID of this thread or 0 if it has not been started. (...)

Why not set the attribute to None before a thread starts? It would be more consistent with the the "ident" attribute behavior, no? Extract __repr__():

    def __repr__(self):
        assert self._initialized, "Thread.__init__() was not called"
        status = "initial"
        if self._started.is_set():
            status = "started"
        ...
        if self._ident is not None:
            status += " %s" % self._ident
        return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)

"is None" is a reliable check to decide if the attribute is set or not.

With the current implementation, it's hard to guess since PyThread_get_thread_native_id() returns on some platforms... But again, I would prefer to not have the attribute if PyThread_get_thread_native_id() is not available on a platform.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36084>
_______________________________________


More information about the Python-bugs-list mailing list