Is the description of threading.Thread.is_alive a bit confusing?

The is_alive() method in threading module, is described as this:

This method returns True just before the run() method starts until just after the run() method terminates.

Doesn’t that mean is_alive return True before the run() method starts? I am not a native speaker, so I am not sure whether it is indeed worded in a confusing way or just I didn’t quite understand it.

It says “just before” and “just after”. In colloquial English this
implies as close to the start of run() as possible and as close to the
end as possible…

I expect that the module endeavours to ensure that is_alive() returns
True for the entire period when run() is running, but as close to
that as possible. Consider how this is probably implemented

     T._is_alive = True
         T._is_alive = False

That sets the flag then immediately runs the function. But technically
the flag will be set before the function commences, and cleared

Cameron Simpson

I agree that the wording in the documentation could be better, but you got it right:

Yes. When the docs say that is_alive() returns True just before run() starts, it means that Python will first set a flag to indicate that the thread is alive, and then start executing the run() method. That is, there is a brief period of time in which the flag has been set, but the run() method has not yet been called.

1 Like

You missed the first statement that is key.

Return whether the thread is alive.

The logic is likely

  1. Thread starts
  2. is_alive = True
  3. run() called
  4. is_alive = False
  5. Thread exits

The purpose is to tell you that the thread is alive, run() may or may not be running. At a high level you can think of the run() being the same as the thread running.

The details are not as simple see

1 Like