Python 如何同时运行两个不定循环,同时更改其中的变量?

Python 如何同时运行两个不定循环,同时更改其中的变量?,python,multithreading,ffmpeg,Python,Multithreading,Ffmpeg,我试图用Python编写一个脚本,实时记录IP摄像机的流。它只保留大约一分钟的记录,不断覆盖相同的文件。每当触发外部传感器时,我希望设置一个变量(事件变量),该变量将记录与传感器触发后记录的额外30秒合并。然后将组合的90秒保存为日期和时间,以供以后查看 想法是有两个不确定的while循环,第一个包含实时记录和事件。第二个将持续读取输入并激活“事件”功能。起初我以为我可以用我以前学过的硬件中断的软件版本,但经过一些研究后,似乎这只是个例外。我目前正在使用TkInter,用按键模拟外部输入 当我尝

我试图用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"
i=0
事件=错误
aboutToQuit=False
someVar='事件已停用'
lastVar=False
def流程(框架):
打印“线程”
i=0
帧=帧
frame.bind(“,开关)
frame.bind(“,退出)
frame.pack()
frame.focus_set()
def开关(事件):
打印(['Activating','disactivating'][event])
事件=事件
事件=非(事件)
def出口(事件):
打印“退出应用程序”
全球关于戒烟
#aboutToQuit=True
root.destroy()
打印(“导入的文件是”,tkinter.\uuuu文件\uuuu)
def GetTime():#函数打开一个脚本,将最终合并的文件保存为日期和时间。
time=datetime.datetime.now()
子进程调用(“./GetTime.sh”,shell=True)
返回(时间)
def主(根):
全局事件,aboutToQuit,someVar,lastVar
而(不是关于退出):
root.update()#始终处理新事件
如果事件==假:
someVar='事件已停用'
subprocess.call(Last30S#u命令)#记录覆盖自身的最后30秒。
打印“立即合并”
subprocess.call(Merge_命令)#将前30秒与旧30秒合并
打印“立即换班”
subprocess.call(Shift_命令)#将最近30秒的录制复制到旧的30秒,并覆盖它。
如果lastVar==True:#仅在lastVar状态更改时触发
打印someVar
lastVar=False
时间。睡眠(.1)
如果事件==真:
someVar='Event Activated'
打印“记录事件”
子流程调用(EventRecord#u命令)#记录事件触发后30秒。
打印“立即合并事件”
子进程调用(EventMerge_命令)#将Merge_命令的1分钟录制与EventRecord_命令的30秒录制合并
如果lastVar==False:
打印someVar
lastVar=True
时间。睡眠(.1)
GetTime()#将90秒的EventMerge_命令保存为日期和时间。
subprocess.call(EventShift_命令)#将EventRecord文件复制到旧的30秒录制,并覆盖它
如果要退出:
打破
如果名称=“\uuuuu main\uuuuuuuu”:
root=Tk()
框架=框架(根,宽度=100,高度=100)
#maintthread=threading.Thread(目标=main(根))
#inputthread=threading.Thread(目标=进程(帧))
#inputthread.daemon=True
#inputthread.start()
#maintthread.daemon=True
#maintthread.start()
#maintthread.join()
进程(目标=进程(帧)).start()
进程(目标=主(根)).start()
打印“主循环”

两个进程不会共享数据,因此每个进程将包含这些全局变量的一个副本,这对您不起作用


最好的选择是线程或协同例程(gevent)。我假设您的逻辑是->记录30秒,检查事件是否触发,如果触发,再记录30秒并合并。也就是说,我假设您不需要在事件触发后立即停止记录。

两个进程不会共享数据,因此每个进程将包含这些全局变量的副本,这对您不起作用


最好的选择是线程或协同例程(gevent)。我假设您的逻辑是->记录30秒,检查事件是否触发,如果是,再记录30秒并合并。也就是说,我假设您不需要在事件触发后立即停止记录。

看看python中的线程。也可以考虑锁,如果变量的改变应该是相同的或相互影响的。看看Python中的线程。也可以考虑锁,如果变量的改变应该是相同的或相互影响的。