运行 Python-脚本无限
Run Python-Script infinitely
我尝试了我在 Internet 上找到的所有内容,但无法按我想要的方式运行。我把这个 Python-Script 拼凑在一起,效果非常好。
除了:
它不是无限工作的。我必须将 time.sleep() 设置为较高的数字,否则脚本将退出。但是虽然脚本 运行s 它有效。我怎样才能让脚本 运行 无限? :)
这是完整的脚本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
import logging
logging.basicConfig(level=logging.INFO)
sys.path.append('../')
from obswebsocket import obsws, events
host = "blabla"
port = 4444
password = "BLABLA"
#
# Scene Config
#
scene1 = "HDMI1"
scene2 = "HDMI2"
scene3 = "HDMI3"
scene4 = "HDMI4"
scene5 = "Desktop+HDMI1"
scene6 = "Desktop+HDMI2"
scene7 = "Desktop+HDMI3"
scene8 = "Desktop+HDMI4"
#
# Configure Tallys
#
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
def on_switch(message):
obsscene = message.getSceneName()
if obsscene == scene1:
print ("Kamera 1 aktiv")
GPIO.output(18, GPIO.HIGH)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
elif obsscene == scene2:
print ("Kamera 2 aktiv")
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
elif obsscene == scene3:
print ("Kamera 3 aktiv")
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.HIGH)
GPIO.output(25, GPIO.LOW)
elif obsscene == scene4:
print ("Kamera 4 aktiv")
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.HIGH)
else:
print ("Szene ohne Tally:")
print (obsscene)
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
ws = obsws(host, port, password)
ws.register(on_switch, events.SwitchScenes)
ws.connect()
try:
print("OK")
time.sleep(999999999999999999999999)
print("END")
except KeyboardInterrupt:
pass
改为在底部试试这个
try:
While True:
except KeyboardInterrupt:
pass
所以这解决了问题:
try:
while True:
pass
except KeyboardInterrupt:
pass
我尝试了我在 Internet 上找到的所有内容,但无法按我想要的方式运行。我把这个 Python-Script 拼凑在一起,效果非常好。
除了:
它不是无限工作的。我必须将 time.sleep() 设置为较高的数字,否则脚本将退出。但是虽然脚本 运行s 它有效。我怎样才能让脚本 运行 无限? :)
这是完整的脚本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
import logging
logging.basicConfig(level=logging.INFO)
sys.path.append('../')
from obswebsocket import obsws, events
host = "blabla"
port = 4444
password = "BLABLA"
#
# Scene Config
#
scene1 = "HDMI1"
scene2 = "HDMI2"
scene3 = "HDMI3"
scene4 = "HDMI4"
scene5 = "Desktop+HDMI1"
scene6 = "Desktop+HDMI2"
scene7 = "Desktop+HDMI3"
scene8 = "Desktop+HDMI4"
#
# Configure Tallys
#
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
def on_switch(message):
obsscene = message.getSceneName()
if obsscene == scene1:
print ("Kamera 1 aktiv")
GPIO.output(18, GPIO.HIGH)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
elif obsscene == scene2:
print ("Kamera 2 aktiv")
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
elif obsscene == scene3:
print ("Kamera 3 aktiv")
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.HIGH)
GPIO.output(25, GPIO.LOW)
elif obsscene == scene4:
print ("Kamera 4 aktiv")
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.HIGH)
else:
print ("Szene ohne Tally:")
print (obsscene)
GPIO.output(18, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
ws = obsws(host, port, password)
ws.register(on_switch, events.SwitchScenes)
ws.connect()
try:
print("OK")
time.sleep(999999999999999999999999)
print("END")
except KeyboardInterrupt:
pass
改为在底部试试这个
try:
While True:
except KeyboardInterrupt:
pass
所以这解决了问题:
try:
while True:
pass
except KeyboardInterrupt:
pass