-
-
Save Raka-loah/07eebf739c6273432ec30b1073c8d63b to your computer and use it in GitHub Desktop.
Same as memory_log.py but runs in background with 2 different jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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