繁体   English   中英

从 python 清除 memory 缓存

[英]Clear memory cache from python

我正在使用 python 因为我尝试在 for 循环中从冷 state 中的 psql 运行一些查询。所以在执行每个查询之前,我的缓存必须清楚:我导入了操作系统,然后我这样做了:

if state=="cold":
         os.system('sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"')

我得到这个 output:

sh: 1: cannot create /proc/sys/vm/drop_caches: Permission denied

sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper

我不知道如何解决这个问题。

根据这个堆栈问题:

如何从 python 脚本传递 sudo 密码

你可以试试这个:

password = 'my sudo password'
command = 'sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"'

# formatting the sudo password and the command
shell_script = f"echo {password} | sudo -S {command}"

# running the script
os.system(shell_script)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM