我正在尝试用Python编写一个脚本来实时记录IP摄像机的流。它只保留大约一分钟的记录,不断重写相同的文件。每当触发外部传感器时,我希望设置一个变量(事件变量),它将记录与传感器触发后额外30秒的记录合并在一起。然后将合并的90秒保存为日期和时间,供以后查看。在
这个想法是有两个不确定的while循环,第一个循环同时包含实时记录和事件。第二个将持续读取输入并激活“事件”功能。一开始我想我只需要一个以前学过的硬件中断的软件版本,不过经过一些研究后,似乎只有例外情况。我目前正在使用TkInter,用按键模拟外部输入。在
当我尝试它时,输入不会导致事件被触发。所以我的问题是:如何同时运行两个不确定的循环,同时让输入循环改变主循环中的变量,以便“事件”能够实时发生?在
因为我使用ffmpeg来记录流,一旦命令被调用来记录它就不能被停止,但是我希望事件变量尽快被改变。在
我已经看过几个关于多个循环的类似问题,并尝试过多处理(虽然这似乎只用于性能,这在这里并不重要),制作两个独立的文件(不确定如何让它们一起工作),最后,线程。这些在我的情况下似乎都不管用,因为我无法让它们按我想要的方式运行。在
这是我最近的一次尝试,使用线程:
i = 0
event = False
aboutToQuit = False
someVar = 'Event Deactivated'
lastVar = False
def process(frame):
print "Thread"
i = 0
frame = frame
frame.bind("<space>", switch)
frame.bind("<Escape>", exit)
frame.pack()
frame.focus_set()
def switch(eventl):
print(['Activating','Deactivating'][event])
event = eventl
event = not(event)
def exit(eventl):
print'Exiting Application'
global aboutToQuit
#aboutToQuit = True
root.destroy()
print("the imported file is", tkinter.__file__)
def GetTime(): #function opens a script which saves the final merged file as date and time.
time = datetime.datetime.now()
subprocess.call("./GetTime.sh", shell = True)
return (time)
def main(root):
global event, aboutToQuit, someVar,lastVar
while (not aboutToQuit):
root.update() # always process new events
if event == False:
someVar = 'Event Deactivated'
subprocess.call(Last30S_Command) #records last 30 seconds overwriting itself.
print "Merge now"
subprocess.call(Merge_Command) #merges last 30 seconds with the old 30 seconds
print "Shift now"
subprocess.call(Shift_Command) #copies the last30s recording to the old 30 seconds, overwriting it.
if lastVar == True: #Triggers only when lastVar state changes
print someVar
lastVar = False
time.sleep(.1)
if event == True:
someVar = 'Event Activated'
print"Record Event"
subprocess.call(EventRecord_Command) #records 30 seconds after event is triggered.
print"EventMerge Now"
subprocess.call(EventMerge_Command) # merges the 1 minute recording of Merge_Command with 30 seconds of EventRecord_Command
if lastVar == False:
print someVar
lastVar = True
time.sleep(.1)
GetTime() #Saves 90 seconds of EventMerge_Command as date and time.
subprocess.call(EventShift_Command) #Copies EventRecord file to the old 30 second recording, overwriting it
if aboutToQuit:
break
if __name__ == "__main__":
root = Tk()
frame = Frame(root, width=100, height=100)
# maintthread = threading.Thread(target=main(root))
# inputthread = threading.Thread(target=process(frame))
# inputthread.daemon = True
# inputthread.start()
# maintthread.daemon = True
# maintthread.start()
# maintthread.join()
Process(target=process(frame)).start()
Process(target=main(root)).start()
print "MainLoop"
两个进程不会共享数据,因此每个进程将包含这些全局变量的一个副本,这对您不起作用。在
最好的选择是线程或协同例程(gevent)。我假设你的逻辑是->记录30秒,检查事件是否触发,如果是,再记录30秒并合并。i、 我假设你不需要在事件一触发就停止录制。在
相关问题 更多 >
编程相关推荐