yannbane
yannbane

Reputation: 11

How do I exit a Python program that has a thread running?

I'm writing some simple code and I use Pygame for graphics and I have a thread that draws everything. When I call sys.exit() or just ctrl+c, the main program exits but the thread seems to still be alive. I think I need to shut it down before. How do I do that?

Example:

import threading

class Bla(threading.Thread):
    def run(self):
        print "I draw here"

Bla().start()

while True:
     if user_wants_to_end:
         sys.exit()

And when I do want to exit the program doesn't exit! How do I shut the thread down?

Upvotes: 1

Views: 2687

Answers (1)

Miki Tebeka
Miki Tebeka

Reputation: 13850

Python programs exit when all non-daemon threads are done.

class Bla(threading.Thread):
    daemon = True # Make the thread daemon
    def run(self):
        print "I draw here"

Upvotes: 2

Related Questions