python - 我想停止线程时不停止

标签 python multithreading opencv tkinter python-multithreading

我有一个程序可以在tkinter窗口中显示视频。当我想关闭窗口时,我停止了视频线程。但这并没有停止。有四个功能:

    def start_video(self):
        if self.video is not None:
            self.video_thread_stopped = False
            try:
                if not self.video_thread.is_alive():
                    self.video_thread = threading.Thread(target=self.video_stream)
                    self.video_thread.start()
            except AttributeError:
                if self.video_thread is None:
                    self.video_thread = threading.Thread(target=self.video_stream)
                    self.video_thread.start()
start_video函数启动视频线程
    def video_stream(self):
        _, frame = self.video.read()
        str_time = time.time()
        while frame is not None and getattr(self.video_thread, "running", True):
            self.current_frame += 1

            # resize and set image to label
            frame = cv2.resize(frame, self.video_frame_size)
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
            imgtk = ImageTk.PhotoImage(image=Image.fromarray(cv2image))
            self.video_label.config(image=imgtk)
            self.video_label.image = imgtk

            # waiting for frame rate
            while time.time() - str_time < (1 / self.fps):
                pass
            str_time = time.time()

            # reading next frame
            _, frame = self.video.read()
        print("exited from loop")
video_stream是线程函数
    def pause_video(self):
        if self.video_loaded:
            self.video_thread_stopped = True
            if self.video_thread is not None:

                # stop video
                self.video_thread.running = False
                print('before join')
                start_time = time.time()
                while self.video_thread.is_alive():
                    if time.time() - start_time > 1:
                        break
                print("after while")
                self.video_thread.join()
                print('after join')
pause_video必须终止线程并停止流视频
    def on_window_close(self):
        self.pause_video()
        print("thread stopped")
        self.root.destroy()
on_window_close必须在关闭tk窗口之前停止线程
(我有以下代码)
root.protocol("WM_DELETE_WINDOW", w.on_window_close)
因此,当我启动视频线程并按tk窗口上的关闭按钮时,线程不会停止。这是终端输出
before join
after while
有人可以帮我一下,为什么不能停止video_thread。谢谢!

最佳答案

我找到了解决方案。
在start_video方法中,您必须创建video_thread守护程序:

    def start_video(self):
        if self.video is not None:
            self.video_thread_stopped = False
            try:
                if not self.video_thread.is_alive():
                    self.video_thread = threading.Thread(target=self.video_stream)
                    self.video_thread.daemon = 1
                    # start audio
                    self.audio_class.start(self.current_frame, self.total_video_frames)
                    # start thread
                    self.video_thread.start()
            except AttributeError:
                if self.video_thread is None:
                    self.video_thread = threading.Thread(target=self.video_stream)
                    self.video_thread.daemon = 1
                    # start audio
                    self.audio_class.start(self.current_frame, self.total_video_frames)
                    # start thread
                    self.video_thread.start()
在pause_video方法中,您需要使用video_thread.join()删除行
    def pause_video(self):
        if self.video_loaded:
            self.video_thread_stopped = True
            if self.video_thread is not None:
                # stop audio

                # stop video
                self.video_thread.running = False
                print("video stopped")
                self.audio_class.stop()
                print("audio stopped")
                # print('before join')
                start_time = time.time()
                while self.video_thread.is_alive():
                    # print("waiting for none")
                    if time.time() - start_time > 1:
                        # self.video_thread = None
                        break

关于python - 我想停止线程时不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64459568/

相关文章:

python - 使用 python 在文件中搜索回文并将其打印在列表中

python - Google App Engine - 使用任务队列或延迟作业

c++ - 使用 std::vector 和内存释放

c++ - 如何使用 PortAudio 和 OpenCV 避免不一致的音频播放?

Python:简单的 ctypes dll 加载会产生错误

Python:筛选充满 unicode 的列表并将列表仅包含字符串

java - 在多核处理器环境中,是否可以有多个线程在同一给定时间运行相同的静态方法

java - 关于线程中止和死锁的问题 - volatile 关键字

java - 如何使用处理在单独的线程中读取 OpenGL 像素?

c++ - opencv中的跳跃视频处理