【问题标题】:Killing or stopping an active thread杀死或停止活动线程
【发布时间】:2015-01-28 20:21:56
【问题描述】:

我已经走了很长一段路,而且我快到了。我已经从使用 Thread 转换为 Threading,现在可以在播放过程中切换视频,但我仍然无法杀死或停止第一个视频。基本上,我正在使用 OMXplayer 制作一个由 Raspberry Pi 上的按钮控制的视频播放器。目前,我必须等待一个视频播放完毕才能按下另一个按钮,否则它会因为同时播放多个视频而崩溃。

非常感谢你们提供的任何帮助。

#!/usr/bin/python

import RPi.GPIO as GPIO
import subprocess
import threading
import time

GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)

GPIO.setup(9, GPIO.IN)  # Button 1
GPIO.setup(10, GPIO.IN) # Button 2

def shoppingcart():
        global current_video
        while True:
                if GPIO.input(9):
                        #current_video.terminate()
                        #current_video.kill()
                        print "Play Shoppingcart"
                        time.sleep(1)
                        current_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],
                                        stdin=subprocess.PIPE,stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE,close_fds=True)

def dodgeballs():
        global current_video
        while True:
                if GPIO.input(10):
                        #current_video.terminate()
                        #current_video.kill()
                        print "Play Dodgeballs"
                        time.sleep(1)
                        current_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],
                                        stdin=subprocess.PIPE,stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE,close_fds=True)

v1 = threading.Thread( name='shoppingcart', target=shoppingcart ) # Videos thread
v2 = threading.Thread( name='dodgeballs', target=dodgeballs )   # Videos thread

v1.start()
v2.start()

while True:
        pass

GPIO.cleanup() #Reset GPIOs

【问题讨论】:

  • 使用thread.stop()
  • 我读了你的一些代码......为什么不使用同一个线程?而且python中没有thread.stop()docs.python.org/2/library/threading.html#thread-objects
  • 我的意思是你可以通过继承 Thread 并使用布尔哨兵来实现它。
  • 是的,你可以......但我想他想做的下一件事是打开另一个线程。何必? :)
  • @ReutSharabani 我只是尝试将它们放在同一个线程中,但我仍然需要一种方法来在下一个视频播放之前停止子进程或视频。我将它们放在单独的线程中,因为我试图杀死线程而不是子进程。

标签: python multithreading raspberry-pi gpio omxplayer


【解决方案1】:

您需要实现自己的线程:

class RaspberryThread(threading.Thread):
    def __init__(self, function):
        self.running = False
        self.function = function
        super(RaspberryThread, self).__init__()

    def start(self):
        self.running = True
        super(RaspberryThread, self).start()

    def run(self):
        while self.running:
            self.function()

    def stop(self):
        self.running = False

然后从函数中删除 while 循环,以便将它们传递给线程。

v1 = RaspberryThread(function = shoppingcart)
v2 = RaspberryThread(function = dodgeballs)

v1.start()
v2.start()

然后你可以随时停止线程

v1.stop()
v2.stop()

【讨论】:

  • 我刚刚重新启动,仍然没有视频...如此接近!当我按下每个按钮时,它会在“玩躲避球”和“玩购物车”之间关闭。我会玩 omxplayer。它以前工作得很好,但不知道。再次感谢您的帮助。
  • 我告诉你这是你的视频播放器的问题。为什么不试试用 Pygame 玩呢?
  • 我会试试的。我以前也这样做过,但因为它看起来更简单,所以改用 omxplayer。
  • 如果您需要其他任何东西,请告诉我,但为了知道我建议使用万无一失的命令行工具来播放媒体。
  • 我会试试的。我必须使用控制视频的 GPIO 按钮来运行它。这是我知道的唯一方法。如果你知道另一种方法一定要打我!谢谢! jmcclaire [at] gmail [dot] com
猜你喜欢
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多