变量更改时的python信号?

Gui*_*ume 5 python variables

我有一个脚本,它每秒计算一个变量(我的程序读取脚本 bash 的输出并每秒解释数据)。是否存在检测此 var 更改的方法?

这是我的代码的一部分,文本是 ffmpeg 或 avconv 输出,从 vte 终端读取:

#Terminal
def terminal(self):
        self.v = vte.Terminal()
        self.v.connect ("child-exited", lambda term: self.verif(self, my_class))
        self.v.connect('contents-changed', self.term_output)
        [...]    
def term_output(self, my_class, donnees=None):
        text = str(self.v.get_text(lambda *a: True).rstrip())
        [...] # decode the text
        print "time", self.time
        print "duration", self.duration
Run Code Online (Sandbox Code Playgroud)

vte终端中的返回(avconv输出):

#Terminal
def terminal(self):
        self.v = vte.Terminal()
        self.v.connect ("child-exited", lambda term: self.verif(self, my_class))
        self.v.connect('contents-changed', self.term_output)
        [...]    
def term_output(self, my_class, donnees=None):
        text = str(self.v.get_text(lambda *a: True).rstrip())
        [...] # decode the text
        print "time", self.time
        print "duration", self.duration
Run Code Online (Sandbox Code Playgroud)

输出示例(它是来自 vte 输出的时间和持续时间):

Duration: 00:00:23.00, start: 0.100511, bitrate: 0 kb/s
Output #0, matroska, to '/media/guillaume/XT/Telechargements/uzz/la_qualite_de_l_air_1000019643.mkv':
Press [q] to stop, [?] for help
frame=  589 fps=115 q=-1.0 Lsize=    2191kB time=00:00:23.79 bitrate= 754.3kbits/s
FIN DU TRAITEMENT
Votre Fichier Final Est:
/media/guillaume/XT/Telechargements/uzz/la_qualite_de_l_air_1000019643.mkv
Run Code Online (Sandbox Code Playgroud)

Ans*_*eek 3

添加一个标志变量(全局或局部,取决于所需的范围)。

在开始/第一个循环中将值分配给标志。

将此标志的值与变量进行比较,看看它是否发生变化(无论您需要什么信号或打印)。

time_val = 'init';

def term_output(self, my_class, donnees=None):
    text = str(self.v.get_text(lambda *a: True).rstrip())
    [...] # decode the text
    if (time_val == 'init'):
        time_val = self.time
    if self.time != time_val:
        print "The value of time has changed from " + str(time_val) + " to " + str(self.time)
    print self.time
Run Code Online (Sandbox Code Playgroud)