后台守护进程用python使用time时踩的坑
后台守护进程 使用python时间使用坑
- 待引入包内容 ttime.py
import time
c = time.strftime('%Y%m%d', time.localtime())
print(c)
- 后台守护 changzhutest.py
from ttime import c
import time
while True:
print("timenow",time.strftime("%Y-%m-%d %H:%M:%s",time.localtime()))
print('now:',time.strftime('%Y%m%d', time.localtime()))
print('import C :',c)
time.sleep(1200)
- 执行
python -u changzhutest.py >test.log &
- 执行后内容输出
说明:代码中引入的时间没有问题,使用import导入的变量获取的时间没有变化
('timenow', '2020-08-13 23:46:1597333602')
('now:', '20200813')
('import C :', '20200813')
('timenow', '2020-08-14 00:06:1597334802')
('now:', '20200814')
('import C :', '20200813')
结论
使用应用做后台守护时要注意:import过来的数据使用time获取时需要注意,尽量在程序执行代码中使用,而不是导入变量中。