python定时器教程_python定时器的实践

背景:

某一天发现某个andriod机器的温度过高,andriod工程师告诉我cpu的温度在/sys/class/thermal/中。那么如何监控温度呢?

构思:

利用定时器和adb命令获取机器的cpu温度。

准备:

首先我们做一些准备

adb是什么?

adb即andriod-debug-bridge的缩写。中文叫andriod调试桥,adb是一个功能多样的命令行工具。通过这些命令我们可以与设备交互。

adb命令

adb connect +ip #远程连接设备

adb disconnect #断开连接

adb devices #查看连接

adb shell #远程登录到设备

threading.Timer

Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法。Timer从Thread派生,没有增加实例方法。

函数:Timer(interval, function, args=[ ], kwargs={ })

interval: 指定的时间

function: 要执行的方法

args/kwargs: 方法的参数

开始

import os

import threading

ip= '192.168.0.200'

os.system('adb disconnect')

os.system('adb connect '+ip)

list=[]

def fn():

#f1= os.system('adb cat /sys/class/thermal/thermal_zone1/temp')

#popen支持读取,system不支持读取

out = os.popen('adb shell cat /sys/class/thermal/thermal_zone1/temp').readline()

timer = threading.Timer(5, fn)

timer.start()

list.append(str(out))

print(list)

if __name__ == '__main__':

fn()

tips:

1、当设备已经被连接时其他的adb客户端将不能连接,故要先断开其他设备的连接。

2、os模块中 popen支持读取,system不支持读取。所以读取时要使用popen

测试的乐趣是测试进步的不竭动力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值