Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用线程方法扩展(可停止)线程子类_Python_Multithreading_Class_Video_Python Multithreading - Fatal编程技术网

Python 使用线程方法扩展(可停止)线程子类

Python 使用线程方法扩展(可停止)线程子类,python,multithreading,class,video,python-multithreading,Python,Multithreading,Class,Video,Python Multithreading,我正在尝试创建threading.Thread的子类,其方法是线程化的。我将它用于视频,但我怀疑一个工作示例通常对人们有用 我在这里意识到,我从未实例化过线程,也从未调用过start()方法,但我不知道从何处调用它,也不知道如何调用它。我还想保存线程句柄,以便在收到stop()信号时停止它 import threading class VideoThread(threading.Thread): """Thread class with a stop() method. The thr

我正在尝试创建threading.Thread的子类,其方法是线程化的。我将它用于视频,但我怀疑一个工作示例通常对人们有用

我在这里意识到,我从未实例化过线程,也从未调用过
start()
方法,但我不知道从何处调用它,也不知道如何调用它。我还想保存线程句柄,以便在收到
stop()
信号时停止它

import threading

class VideoThread(threading.Thread):
    """Thread class with a stop() method. The thread itself checks
    regularly for the stopped() condition."""

    def __init__(self, playlist=None):
        super(VideoThread, self).__init__()
        self._stop = threading.Event()
        self._player_pgid_list = []
        if playlist:
            self.start_sequence(playlist)

    def stop(self):
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

    def start_sequence(self, playlist):
        if not isinstance(playlist, list):
            raise ValueError("Expecting a list")
        for video in playlist:
            if not self.stopped():
                self.__start_video__(video)

    def __start_video__(self, video):
        if not isinstance(video, dict):
            raise ValueError("Expecting a dictionary of video data")
        # start the video
        # store the video pgid so we can kill it if we have to
        # tight wait loop to check for stopped condition
        # kill all video(s) if necessary using the stored pgids
该类尽可能地工作,但当然,没有一个方法实际上是线程化的

start\u sequence()
是公共的,因此我可以像下面这样启动视频的线程序列:

video = VideoThread()
video.start_sequence([films[1], films[3], films[2]])
video = VideoThread([films[1], films[3], films[2]])
或者当我像这样实例化类时:

video = VideoThread()
video.start_sequence([films[1], films[3], films[2]])
video = VideoThread([films[1], films[3], films[2]])
以后,如果我需要停止,我可以:

video.stop()

我缺少什么?

您应该将
start\u sequence
方法重命名为
run
并删除
playlist
参数(改用
self.playlist
)。另外,删除
方法中的最后两行。我的意思是:

class VideoThread(threading.Thread):


    def __init__(self, playlist=None):
        super().__init__()
        self._stop = threading.Event()
        self._player_pgid_list = []
        self.playlist = playlist

    def run(self):
        if not isinstance(self.playlist, list):
            raise ValueError("Expecting a list")
        for video in self.playlist:
            if not self.stopped():
                self.__start_video__(video)

    ...
然后,要使用您的类,只需执行以下操作:

playlist = VideoThread(films)
playlist.start()
您可以通过以下方式停止:

playlist.stop()

请注意,当您调用
.start
时,它会在一个单独的控制线程中调用
run
方法,请检查以获取更多信息。

使用此方法,我可以像在
VideoThread(films).start()中一样链接
start()
?是的,您可以,但是如果你这样做,你就失去了对线程的引用,这意味着你以后将无法停止它。就我个人而言,这是我不会做的。