Reputation: 2112
I found this article about queue's in python: http://www.blog.pythonlibrary.org/2012/08/01/python-concurrency-an-example-of-a-queue/
It works fine but I have question about it. In the run method of the thread we see:
def run(self):
while True:
# gets the url from the queue
url = self.queue.get()
# download the file
self.download_file(url)
# send a signal to the queue that the job is done
self.queue.task_done()
The thread is in a infinite while-loop without calling break. What happens to this thread when the program ends?
Upvotes: 1
Views: 580
Reputation: 94961
It depends on what you mean by "the program ends". If the main thread just reaches the end of its execution, there are two possibilities:
If the thread is a non-daemon threading.Thread
, the running thread will keep the program alive indefinitely.
If the thread is a daemon, the thread will be abruptly terminated as soon as the main thread ends.
The daemon
property is described in the docs like this:
A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when no alive non-daemon threads are left.
If by "program ends" you mean the process gets a SIGTERM or SIGKILL or something similar, then the thread will terminate along with the rest of the program, even if its not a daemon
.
Upvotes: 2