Mokus
Mokus

Reputation: 10400

Create daemon thread

I'm trying to create a daemon thread under Windows, but I have no clue what am I doing wrong. The code below is acting as a normal thread: I don't see "End run" written to the console. Any suggestions?

def start(self):         
    self.isrunning = True        
    self.thread = threading.Thread(name="GPS Data", target=self.thread_run)
    self.thread.setDaemon(True)
    self.thread.run()
    print "End Run"


def thread_run(self):
    while self.isrunning:
        data  = self.readline()
        print(data)

Upvotes: 1

Views: 255

Answers (1)

NPE
NPE

Reputation: 500733

The following:

self.thread.run()

should read:

self.thread.start()

Otherwise, thread_run() is getting called in the context of the current thread, and not in the context of a new thread.

The thread_run() function never returns (because self.isrunning never changes), and the code never reaches the print statement.

Upvotes: 6

Related Questions