One additional question:
Your sample routines contain USER = [xxx]
statements and I am assuming that they are simply labels to indicate what’s happening.
I also am assuming that both of the sample programs and the class instance are running as, and are owned by, the “pi” user.
I am also assuming that any file created by the pi user is modifiable by the pi user.
I also assume that the mutex works because “user 2” cannot open the mutex for write access when “user 1” has it.
Question:
Why?
Both processes are owned by the pi user so they should both have write access to the same file, right?
However, it appears that the second process, despite being owned by “pi”, cannot write to an open file it owns.
Possible answer:
open([path/filename], w)
returns a file handle, (a numerical pointer to the open file instance), as in FILE_HANDLE = open([path/filename], w)
and you access the file using something like write(FILE_HANDLE, bytes)
.
If a second process, or even a different place in the same process, tries to “open” the same file again via FILE_HANDLE2 = open([path/filename], w)
, since the file is already open and has an assigned file handle, it can’t be re-opened for write a second time unless the first instance has been formally closed via close(FILE_HANDLE)
.
Am I correct?
Additional question:
I know that variables can exist within a local function/class scope and multiple instances of the same variable can exist due to scoping rules.
Can there be files with local scope?
Class(Test_Class, [etc.])
[various statements]
def(method, [xxx])
HANDLE =open([path/filename], w)
(do something with it)
[more stuff]
(end of class definition)
Process_1
Instance = Test_Class([parameters])
[do something using the file]
etc
Process_2 (which can run in parallel with process 1)
Instance_2 = Test_Class([parameters])
[do something using the file]
etc
Are the two file instances local to the class instances or do they reference the same object if [path/file] are identical?
My assumption is that files exist independently of the object creating them, but are “owned” by the creating object. Therefore the second class instance, if trying to open the same path/filename in an exclusive way (for write) while the first class instance has it open for write will fail and possibly throw an exception.