簡體   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