Home > Enterprise >  Python: lifetime of terminated thread
Python: lifetime of terminated thread

Time:12-24

How long is a thread available after is has terminated?

Background:

I have a CustomThread which extends Threads. Within that CustomThread I store a result. Now I'm curious how long I can access this result after the run() method has finished.

class CustomThread(threading.Thread):

    def __init__(self):

        self.result = None # will be set later in run()
        super().__init__()

CodePudding user response:

there are two separate things,

  1. CustomThread instance which is alive so long as there is a reference to it.
  2. the thread which does the work

the CustomThread instance is only a container that holds data for the thread and will stay alive so long as there is any reference to it, even if the thread that is executing the work dies.

so the answer is: you can access it so long as you can write code that will access it, because then there will be a reference that will keep the data in memory.

def launch_task():
    custom_thread_instance = CustomThread()
    custom_thread_instance.start()
    print(custom_thread_instance.result)  # this is the last line you can access self.result from this function

if you did save custom_thread_instance somewhere then you will be able to access the result later.

CodePudding user response:

First of all, when a thread terminates, it stops executing and releases all of its resources. Once a thread has terminated, you will no longer be able to access its result, as the thread object itself will no longer be available.

But, In case you want to access the result of a thread after it has terminated, you will need to store the result in a separate object that is not tied to the thread :-(.

class CustomThread(threading.Thread):

    def __init__(self):

        self.result = None # will be set later in run()
        super().__init__()

    def run(self):
        # Do something and then assign the result
        self.result = 42


t = CustomThread()
t.start()
t.join()

result = t.result  # You need to do this
print(result)  # prints 42

In the example I provided, the t object will remain in memory as long as there are references to it. Once all references to the t object are removed, it will be eligible for garbage collection, and the memory it occupies will be released.

It's worth noting that the t object itself is separate from the thread that it represents. The thread will terminate when its run method finishes executing, but the t object will remain in memory until it is no longer needed.

CodePudding user response:

I'm curious how long I can access this result after the run() method has finished.

You can access it as long as your program keeps a reference to the CurrentThread instance.

A threading.Thread object is not a thread.* It's not a thread in exactly the same way that a File object is not a file. A file is a persistent thing in the operating system that is backed up by bits on a drive. A File object is, like a dictionary object or a list object, just a thing in your program. It's the thing that your program uses as a "handle" to do things to the operating system's file. The same goes for a Thread object. It's a Python object. It obeys all the same rules as any other Python object, and your program uses it as a handle through which to manipulate an operating system thread.

A Thread object, like any other Python object, ceases to exist only when your program no longer has any way to access it.†


* Nor is any instance of any class like your CurrentThread that inherits from threading.Thread.

† A Thread object can not cease to exist while the thread that it controls is running because even if you've thrown away every explicit reference to it, Python itself keeps Thread object references for all of its running threads. (If it didn't, then what would threading.current_thread() return?)

  • Related