Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who stops my threads?

I have some threads fishing into a queue for jobs, something like this:

class Worker(Thread):
    [...]
    def run(self):
        while not self.terminated:
            job = myQueue.get_nowait()
            job.dosomething()
            sleep(0.5)

Now, self.terminated is just a bool value I use to exit the loop but, this is the problem, several times in a day they stop working without my intervention. All of them but one: the application starts with, lets say, 5 working threads and at random time I check them and one only is working. All the others have both _Thread__initialized and _Thread__stopped fields true. Threads and jobs does not interact with each other. What I should look for?

PS: I understand it's really hard to try to figure out the issue without the actual code, but it's huge.

UPDATE: actually Queue.Empty is the only exception trapped - guess I believed to let all the jobs' internal errors to propagate without kill the threads eheh - so I'm going to block all the exceptions and see...

like image 392
Giorgio Gelardi Avatar asked Feb 27 '23 08:02

Giorgio Gelardi


1 Answers

If that is the actual code it's pretty obvious: myQueue.get_nowait() raises an Exception (Empty) when the queue is empty!

like image 98
Jochen Ritzel Avatar answered Mar 07 '23 20:03

Jochen Ritzel