Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Same as memory_log.py but runs in background with 2 different jobs
from apscheduler.schedulers.background import BackgroundScheduler
from psutil import virtual_memory
from datetime import datetime
def main():
while 1:
print('请输入命令:')
content = input()
# 输入exit退出
if content == 'exit':
exit()
# 输入pause暂停后台任务执行
elif content == 'pause':
scheduler.pause()
# 输入resume继续执行后台任务
elif content == 'resume':
scheduler.resume()
else:
continue
def job():
memory = virtual_memory()
occ_rate = memory.used / memory.total
if occ_rate > 0:
with open('memory_log.txt', mode='a+') as f:
f.write(f'[USED]{datetime.now():%Y-%m-%d %H:%M:%S}: {occ_rate*100:.2f}%\n')
def job2():
memory = virtual_memory()
occ_rate = memory.free / memory.total
if occ_rate > 0:
with open('memory_log.txt', mode='a+') as f:
f.write(f'[FREE]{datetime.now():%Y-%m-%d %H:%M:%S}: {occ_rate*100:.2f}%\n')
if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(job, 'interval', seconds=5)
scheduler.add_job(job2, 'interval', seconds=10)
scheduler.start()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.