本文整理汇总了Python中threading.Timer.run方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.run方法的具体用法?Python Timer.run怎么用?Python Timer.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Timer
的用法示例。
在下文中一共展示了Timer.run方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Launcher
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import run [as 别名]
class Launcher(Frame):
def __init__(self, master=None, game=None):
Frame.__init__(self, master)
self.pack()
self.game = game
self.banner = Label(self)
self.banner.config(text=str(self.game), height=2, font=font.Font(family='Helvetica', size=16, weight='bold'))
self.banner.pack(side="top")
self.board = Board(self, self.game)
self.gameover = Timer(0.1, self.game_over)
self.gameover.run()
def game_over(self):
if self.game.winner is not None:
print("GAME OVER")
else:
print("Continue to play")
示例2: printLog
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import run [as 别名]
os.chdir(dirname)
filename = date.strftime('%Y-%m-%d_%H-%M-%S')
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage('%s.jpg' % filename, img)
os.chdir('..' + os.sep + '..')
printLog('Capture', 'Success ...')
except:
printLog('Capture', 'Falied ...')
def printLog(section, log):
date = datetime.datetime.today()
print '[%s - %s] %s' % (date.strftime('%Y-%m-%d %H:%M:%S'), section, log)
if __name__ == "__main__":
printLog('SYSTEM', 'Starting Look at me ...')
captureFromCam()
sec = 15.0
printLog('SYSTEM', 'Timer Setting ... (%d sec)' % sec)
while True:
t = Timer(sec, captureFromCam)
t.run()
示例3: OpenCloseValve
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import run [as 别名]
class OpenCloseValve(object):
'A class that controls a valve with a close and a open signal'
def man(self, tag, value):
tag = value
self.Documentation(tag, value)
return tag
def Close(self):
self.Man_Close_OUT = True
self.CloseTimer = Timer(self.Time_Close, self.man, [self.Man_Close_OUT, False])
self.CloseTimer.run()
def Open(self):
self.Man_Open_OUT = True
self.OpenTimer = Timer(self.Time_Open, self.man, [self.Man_Open_OUT, False])
self.OpenTimer.run()
def __init__(self):
self.deadband = 2.0
self.Man_Open = False
self.Man_Close = False
self.Man_Close_OUT = False
self.Man_Open_OUT = False
self.Name = 'a'
self.Time_Open = 3.0 # Seconds the valve shall open
self.Time_Close = 2.0 # Seconds the valve shall close
#Declar instances for logging
#self.Write_Stat_Open = Write_temp(self.Man_Open, 'VS1_SV1_Open')
#self.Write_Stat_Close = Write_temp(self.Man_Close, 'VS1_SV1_Close')
self.Control_Time = 0
self.Control_Active = False
self.ControlTimeReset = time.time()
def Documentation(self, direction, value):
with open('Docs/' + self.Name, 'a+') as f:
f.write('{} went {} {} at {}'.format(
self.Name, direction, value, time.time()))
def main(self, PV, SP):
'''In this method the temperatures
are compared, and some control variables
are set for later activaion of the IO
'''
self.deltaT = SP - PV
if self.deadband < self.deltaT:
'If deltaT is bigger than the deadband, open valve and Heat'
#Open
self.Control_Active = True
self.Man_Open = True
self.Man_Close = False
elif self.deltaT > 0 - self.deadband:
'''If deltaT is less than 0 minus deadband,
close valve and Dont heat'''
#close
self.Control_Active = True
self.Man_Close = True
self.Man_Open = False
else:
'If none is true, do nothing'
self.Control_Active = False
self.Man_Close = False
self.Man_Open = False
def control(self):
'''Here the IO are controlled
based on the control variables from
main method
'''
#Can only run Control method once every 10 seconds
if self.Man_Close and self.ControlTimeReset + 10 < time.time() and not self.Man_Open_OUT:
#Run the Close method
self.Close()
#Reset the reset time so it only runs every 10 seconds
self.ControlTimeReset = time.time()
if self.Man_Open and self.ControlTimeReset + 10 < time.time() and not self.Man_Close_OUT:
#Run the Open method
self.Open()
#Reset the reset time so it only runs every 10 seconds
self.ControlTimeReset = time.time()
示例4: DevBase
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import run [as 别名]
class DevBase(object):
def __init__(self, name, url='0.0.0.0', unit=1,
regs_r_address=10000, reg_r_num=0, regs_w_address=0, reg_w_num=0):
# device info
self.summary = str(name) + ' '
self.regsR = []
self.regsRLast = []
self.regsW = []
# devices registers info
self.regsRInter = 1
self.regsWInter = 1
self.unit = unit
self.regsRAddress = regs_r_address
self.regsRNum = reg_r_num
self.regsWAddress = regs_w_address
self.regsWNum = reg_w_num
# data sharing info
# self.filePath = inspect.getfile(inspect.currentframe()) + str(name) + '.dat'
self.filePath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + '/' + str(
name) + '.dat'
self.file = ''
self.normalizeLen = 32
self.create_data_file()
self.mmapData = mmap.mmap(self.file.fileno(), 0)
# devices address info
self.url = url
self.modbusClient = ModbusTcpClient(self.url)
self.readingTimer = Timer(self.regsRInter, self.__read_regs__)
self.writingTimer = Timer(self.regsWInter, self.__send_regs__)
# for internal using
self.regs_r_pos = 0
self.regs_w_pos = self.regs_r_pos + self.regsRNum * self.normalizeLen + 1 # skip the first line and symbol of newline
def create_data_file(self):
open(self.filePath, "w+").close() # create an empty file
self.file = open(self.filePath, 'r+')
for i in range(0, self.regsRNum):
self.file.write(self.normalizeLen * '0')
self.file.write('\n') # the second line for writing
self.file.write('0') # 1 means ready to send regs_w to devices
for i in range(0, self.regsWNum):
self.file.write(self.normalizeLen * '0')
self.file.seek(0)
def __normalize_num__(self, src): # numbers to strings
target = []
for i, d1 in enumerate(src):
d1 = str(d1)
if len(d1) < self.normalizeLen:
target.append(' ' * (self.normalizeLen - len(d1)) + str(d1))
elif len(d1) == self.normalizeLen:
target.append(str(d1))
else:
print('item: \"', d1, '\" index: \"', i, '\" too long...')
target.append(d1[:self.normalizeLen])
return target
def compare_regs_r_last(self):
for i, j in zip(self.regsR, self.regsRLast):
if i != j:
return False
return True
def __write_regs_to_mmap__(self):
data = self.__normalize_num__(self.regsR)
print(data)
for i in range(0, self.regsRNum - 1):
self.mmapData[i * self.normalizeLen:(i + 1) * self.normalizeLen] = bytes(data[i], 'utf8')
def __read_regs__(self):
# print 'reading...'
try:
res = self.modbusClient.read_holding_registers(address=self.regsRAddress, count=self.regsRNum,
unit=self.unit)
self.regsR = res.registers
print('read registers: \t', self.regsR)
if self.compare_regs_r_last(): # write to mmap only the data is different with last time
pass
else:
self.regsRLast = self.regsR
self.__write_regs_to_mmap__()
regs_r_changed()
except pymodbus.exceptions.ConnectionException:
print('Communication failed ...')
finally:
self.readingTimer.run()
#.........这里部分代码省略.........