gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:57:08 25 4
gpt4 key购买 nike

我有一个程序可以在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com