我想每隔5秒将字母X附加到一个列表中。到目前为止我的代码:
import threading
def Seconds():
threading.Timer(5.0, Seconds).start()
List = []
n = "X"
List.append(n)
print List
Seconds()
我希望有一个列表,每5秒增长一次并保存到txt中。每5秒发一次文件。此代码的目的是检查电池供电系统的运行时间。代码与系统一起启动。当电池电量耗尽时,我想读出文本文件,看看代码执行了多长时间,以及我的系统能够用电池运行多长时间。
答案 0 :(得分:0)
独立运行。
def alive():
with open("log.txt", "w") as output:
while True:
time.sleep(5)
output.write("X")
output.flush() # important to ensure that long periods
# of 'X' are not kept in buffer and lost on power down.
p = Process(target=f, args=('bob',))
p.start()
time.sleep(40) # simulate real program
p.terminate() # eventually
答案 1 :(得分:0)
您的嵌入式/电池供电系统是否使用Unix?使用bash脚本可以更轻松地实现这一点:
#!/bin/bash
while true
do
cat /proc/uptime > log.txt
sleep 5
done
然后您可以获得准确的正常运行时间。请注意,在测量此数据时,如果不允许处理器进入较低的电源使用状态并导致额外的磁盘写入,则可能无意中使用了更多的能量。
编辑,一个不依赖于操作系统的粗略Python版本
#!/bin/python
import time
starttime = time.time()
while True:
with open('log.txt', 'w') as log:
print >> log, time.time() - starttime
time.sleep(5)
请注意,该脚本运行前所用的时间将会缩短时间。