Search code examples
pythonpython-3.xpython-multithreading

Python Looping until max number is reached


my tool stops randomly and it seems like all threads are 'ghosts'.

How does it work: The tool loops until the max number of allowed threads at the same time are running, in this case 20. When a thread finishes it starts the next one.

Problem: After like an hour of doing this, the tool is stuck at 20 Threads running but nothing happens anymore.

Thanks in advance everyone!

maxthreadcount = 20
while True:
    if threading.active_count() < maxthreadcount:
            threading.Thread(target=Dealer).start()

Dealer:

def Dealer():
  print("thread started")
  return

Solution

  • You need to terminate previously created threads after their job (print command in this case) is done.

    Take a look at this example from this article:

    class CountdownTask: 
    
        def __init__(self): 
            self._running = True
    
        def terminate(self): 
            self._running = False
    
        def run(self, n): 
            while self._running and n > 0: 
                print('T-minus', n) 
                n -= 1
            time.sleep(5) 
    
    c = CountdownTask() 
    t = Thread(target = c.run, args =(10, )) 
    t.start() 
    ... 
    # Signal termination 
    c.terminate()  
    
    # Wait for actual termination (if needed)  
    t.join()  
    
    

    I think you should call self.terminate() after doing the print. Something like below:

    class Dealer():
        def __init__(self):
            self._running = True
    
        def run(self):
            print("thread started")
            return self.terminate()
    
        def terminate(self):
            self._running = False
    
    

    Edit

    I also believe you can make use of python's ThreadPool to this extent. Instead of spawning threads yourself, you might be able to reuse threads after their assigned task is over, for the new tasks.