OHL*_*ÁLÁ 1 python windows multithreading
我正在尝试在Windows下创建一个守护程序线程,但我不知道我做错了什么.下面的代码充当普通线程:我没有看到写入控制台的"结束运行".有什么建议?
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)
Run Code Online (Sandbox Code Playgroud)
下列:
self.thread.run()
Run Code Online (Sandbox Code Playgroud)
应该读:
self.thread.start()
Run Code Online (Sandbox Code Playgroud)
否则,thread_run()在当前线程的上下文中调用,而不是在新线程的上下文中调用.
该thread_run()功能不会返回(因为self.isrunning永远不会改变),代码永远不会到达print声明.