Noraa
Noraa

Reputation: 35

How to delete thread object from device after finishing the task?

How can I delete thread object after finishing the job? Here's the code for creating the threads, also see that number of threads created reach 100 thread look at the image, how to delete them?

if __name__ == "__main__":
    segmentationProcess = SegmentationProcess()
    data = segmentationProcess.readDoc("./IT resumes as text/ktalzoubi.txt")
    data = segmentationProcess.preProcess(data)
    lock = multiprocessing.Lock()
    thread1 = ThreadWithLogAndControls(target=segmentationProcess.searchWorkExperience, args=(data, "W", lock))
    thread2 = ThreadWithLogAndControls(target=segmentationProcess.searchEducation, args=(data, "E", lock))
    thread3 = ThreadWithLogAndControls(target=segmentationProcess.serchSkills, args=(data, "S", lock))
    thread4 = ThreadWithLogAndControls(target=segmentationProcess.searchOthers, args=(data, "O", lock))
    wr1 = weakref.ref(thread1)
    wr2 = weakref.ref(thread2)
    wr3 = weakref.ref(thread3)
    wr4 = weakref.ref(thread4)
    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    print(wr1())
    thread1.join()
    thread2.join()
    thread3.join()
    thread4.join()
    del thread1, thread2, thread3, thread4
    print(wr1())

enter image description here

Upvotes: 1

Views: 1364

Answers (1)

DavidW
DavidW

Reputation: 30931

You're worrying about a non-issue. The text "thread-103" does not mean that there are currently 103 threads. Instead it suggests that >=103 threads have been created. It comes from the Python threading module: if a name isn't supplied for the new thread then it defaults to one like Thread-<number>. <number> only ever goes up, and never goes down even when threads are destroyed (it also doesn't go up if a thread is created with a name, hence the number displayed is only a lower bound). This is only intended to give each thread a recognisable name, and not for anything else.

These threads are probably created in the lines:

data = segmentationProcess.readDoc("./IT resumes as text/ktalzoubi.txt")
data = segmentationProcess.preProcess(data)

We obviously don't know what's in these functions. The threads are probably being cleaned up correctly, but since you don't provide a mininal reproducible example it's impossible to provide any further detail.


There's a secondary point: the point at which Python objects are destroyed are called is pretty unreliable. del does not do so - it only breaks an individual reference to an object. It's likely that there are some circular references to the object stored in thread1 and so it still exists even after the thread1 variable is deled. These circular references are not a problem since they will be caught when the Python garbage collector is run (but exactly when that happens is a arbitrary).

Therefore, resources (other than memory) should not rely on object destruction to free them. The standard Python idiom is to use a with block, or try: ...; finally:. However, in the case of multithreading the resources you should be caring about are released when the thread finishes, so (again) a non-issue. (Or at least an issue out of your control).


Finally you look to be mixing multiprocessing.Lock with threading. These are two different methods of parallelising Python code and I would not assume that it's valid to mix them (it might be, it might not be, but it gives the impression that you haven't really understood what you're doing).

Upvotes: 2

Related Questions