r0edie
r0edie

Reputation: 43

python3 how to delete thread object from list

I'm writing a script that should run indefinitely and put some things in a db every few seconds, using threading. This works, but I see the process' memory slightly increasing every few seconds and I think this is because the list that holds all the Thread objects is never emptied. How would I do that? The join is put under the is_alive condition so it doesn't take any time away from spawning the next threads. The below example results in an

AttributeError: 'Thread' object has no attribute 'kill'

import threading
import time

def dicthing(dic, x):
    dic.update({x: x*x})
    time.sleep(0.01)

dic = {}
threads = []
x = 100
while x > 0:
    t = threading.Thread(target = dicthing, args = (dic, x))
    threads.append(t)
    t.start()
    x -= 1

    if x % 50 == 0:
        print(len(threads), len(threading.enumerate()))

    for t in threads:
        if t.is_alive() == False:
            t.join()
            t.kill = True
    threads = [t for t in threads if not t.kill]

I'd like the output to be:

1 1

1 1

Upvotes: 2

Views: 3850

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10969

Last lines (for-loop and on) can be written either simple as:

    threads_alive = []
    for t in threads:
        if t.is_alive() == False:
            t.join()
        else:
            threads_alive.append(t)

    threads = threads_alive

or if you have to process the already dead threads somehow yet:

    threads_alive = []
    threads_dead = []
    for t in threads:
        if t.is_alive() == False:
            t.join()
            threads_dead.append(t)
        else:
            threads_alive.append(t)

    threads = threads_alive
    for t in threads_dead:
        ... postprocess dead threads here ...

Upvotes: 1

Related Questions