开发者

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

开发者 https://www.devze.com 2023-03-20 19:41 出处:网络
I\'m writing some simple code and I use Pygame for graphics and Ihave 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 ali

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?


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"
0

精彩评论

暂无评论...
验证码 换一张
取 消