Python:使用sudo权限调用函数

Python:使用sudo权限调用函数,python,ipc,pickle,sudo,elevated-privileges,Python,Ipc,Pickle,Sudo,Elevated Privileges,考虑以下情况: def sudo_get_data(): # needs root permissions return some_python_objects() # not plain text output def user_notify(data): # must be called as user if condition(data): desktop_notification(data) while True: # the two func

考虑以下情况:

def sudo_get_data():  # needs root permissions
    return some_python_objects()  # not plain text output

def user_notify(data):  # must be called as user
    if condition(data):
        desktop_notification(data)

while True:  # the two functions are called periodically
    data = sudo_get_data()
    user_notify(data)
    sleep(interval)
我猜我将不可避免地需要拆分代码并在单独的过程中调用
sudo\u get\u data
函数

然而,我不能简单地将数据作为子流程的文本输出,因为出于我的目的,数据被专门构造为Python对象

我可能会对其进行pickle和unpickle,但函数将无限期地每隔几秒钟调用一次,因此不断进行sudo调用不是一个好的解决方案:如果sudoers设置使得用户在每次调用时都会得到提示,该怎么办

到目前为止,我正在考虑在代码的两部分之间创建进程间通信。用户以自己的权限运行主脚本,该主脚本sudo在子流程中运行一个单独的python脚本,然后每隔几秒钟将python数据传回主脚本

  • 这样做有意义吗

    • 如果是这样,什么样的pythonlibs会有用
    • 需要注意的潜在缺点是什么
  • 还有更好的方法吗