2016-10-11 65 views
1

我想在Python中編寫一個腳本,實時記錄IP攝像頭的流。它只保留大約一分鐘的記錄,不斷覆蓋相同的文件。每當外部傳感器被觸發時,我想要設置一個變量(事件變量),該變量將傳感器被觸發後記錄的額外30秒合併。然後將組合的90秒保存爲日期和時間以供稍後檢查。如何同時運行兩個無限循環,同時還要更改其中的變量?

這個想法是有2個無限期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" 
+0

看看python中的線程。同樣考慮鎖定,如果變量變化應該是相同的或者以某種方式相互影響。 – rocksteady

回答

0

兩個進程將無法共享數據,所以每個過程將包含這些全局變量,這會不會爲你工作的一個副本。

最好的選擇是線程或co-routines(gevent)。我假設你的邏輯是 - >記錄30秒,檢查事件是否被觸發,如果是,記錄另外30秒併合並,即假設你不需要在事件觸發時立即停止記錄。