Cenk Ten
Cenk Ten

Reputation: 283

How to handle out-of-memory error while using threads in python

I have memory leak, but I can't find a way to solve it. I think the reason is for that because I use threads and don't stop/kill it in a right way.

I have following method:

import threading
def worker():
     if nextJobActive() and number_of_active_threads<5:
         t = threading.Thread(target=startThread, args=(my_list, my_item))
         t.start() 

def startThread(): 
    #do something here, which takes ~15 Min.

I run the worker() method in while(true) loop. I always have to start new threads in my case. But I never stop a thread. I also don't know how to do this. Is there anyway to safely stop a thread in my case?

Upvotes: 0

Views: 2065

Answers (1)

VRComp
VRComp

Reputation: 131

As you know already, you are creating an endless amount of threads without properly stopping the previous one. To wait for a thread to terminate there is a .join() method. Here is the documentation for the Thread module: docs.

import threading
def worker():
     if nextJobActive() and number_of_active_threads<5:
         t = threading.Thread(target=startThread, args=(my_list, my_item))
         t.start()
         t.join() 

def startThread(): 
    #do something here, which takes ~15 Min.

Upvotes: 1

Related Questions