Consider the following situation:
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)
I'm guessing it's unavoidable that I will need to split up the code and call sudo_get_data
function in a separate process.
Yet I can't simply get data as a text output of a subprocess because the data is specifically structured as Python objects for my purpose.
I could probably pickle and unpickle it, but the functions will be called every few seconds for an indefinite time, so it's not a good solution to make sudo calls constantly: what if sudoers settings are such that the user is prompted on each call?
So far I'm thinking about creating an interprocess communication between the two parts of the code. The user runs the main script with their own privileges, this main script sudo-runs a separate python script in a subprocess, which then communicates python data back to the main script every few seconds.
Does it make sense to do it this way?
- If so, what kind of Python libs would be useful?
- What are the potential disadvantages to be aware of?
Are there any better methods?