我正在尝试编写一个简单的脚本,该脚本将帮助我使用 mqtt 在多个 Raspberry Pi 上开始和停止视频播放。
这是我第一次尝试从 python 脚本启动和停止子进程,但我无法理解正确的操作顺序
有我的代码
import paho.mqtt.client as mqtt
import multiprocessing, time, signal
import os
def player(video):
print("Playing " + video)
os.system("omxplayer-sync " + video)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("player/#")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if msg.topic == "player/PLAY":
p = multiprocessing.Process(target=player, args=[str(msg.payload.decode())])
p.start()
if msg.topic == "player/STOP":
print("Stopping video")
print(p, p.is_alive())
print("Terminating video")
p.terminate()
print(p, p.is_alive())
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_forever()
我可以成功启动视频,但我无法停止它。我很确定这是因为p
对象在回调结束时被销毁,on_message
但我看不到在哪里可以在其他地方创建我的进程。
编辑
这是使用 global 关键字的幼稚方法
import paho.mqtt.client as mqtt
import multiprocessing, time, signal
import os
videoFile = ""
class GracefulExit:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self,signum, frame):
self.kill_now = True
def player():
global videoFile
print("Playing " + videoFile)
os.system("omxplayer-sync " + videoFile)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("player/#")
def on_message(client, userdata, msg):
global videoFile
print(msg.topic+" "+str(msg.payload))
if msg.topic == "player/PLAY":
videoFile = str(msg.payload.decode())
p.start()
if msg.topic == "player/STOP":
print("Stopping video")
print(p, p.is_alive())
print("Terminating video")
p.terminate()
print(p, p.is_alive())
def main():
app_killer = GracefulExit()
while not app_killer.kill_now:
try:
time.sleep(0.5)
except BaseException:
print("Encountered an exeption.")
break
print ("End of the program.")
if __name__ == '__main__':
print("Starting the program")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_start()
p = multiprocessing.Process(target=player)
main()
但我只能播放一次视频并且通话terminate
不会停止播放
编辑 2
现在我p
按照建议声明为全局,但我不知道如何开始播放我从 MQTT 订阅接收的文件名
import paho.mqtt.client as mqtt
import multiprocessing, time, signal
import os
def player():
print("Playing " + "synctest.mp4")
os.system("omxplayer-sync " + "synctest.mp4")
p = multiprocessing.Process(target=player)
class GracefulExit:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self,signum, frame):
self.kill_now = True
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("player/#")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if msg.topic == "player/PLAY":
print("p.args= " + p.name)
videoFile = str(msg.payload.decode())
#how to put the videoFile into the args given to p ?
p.start()
if msg.topic == "player/STOP":
print("Stopping video")
print(p, p.is_alive())
print("Terminating video")
p.terminate()
print(p, p.is_alive())
def main():
app_killer = GracefulExit()
while not app_killer.kill_now:
try:
time.sleep(0.5)
except BaseException:
print("Encountered an exeption.")
break
print ("End of the program.")
if __name__ == '__main__':
print("Starting the program")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_start()
main()