【问题标题】:Clear memory cache from python从python清除内存缓存
【发布时间】:2021-05-30 21:14:09
【问题描述】:

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

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

我得到这个输出:

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

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

【问题讨论】:

  • sudo 需要手动输入密码。除非您使用 -S 选项,否则您无法从代码中输入。试试 -S 选项
  • 并且您的权限被拒绝,因为您没有从 sudo 提示符输入密码,基本上那不是 sudo

标签: python shell caching psql timescaledb


【解决方案1】:

根据这个堆栈问题:

how to pass sudo password from python script

你可以试试这个:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 2012-10-07
    • 1970-01-01
    • 2022-12-20
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多