Looking for general advice on complex program

Chris Angelico rosuav at gmail.com
Sat Jul 16 02:01:16 CEST 2011

On Sat, Jul 16, 2011 at 8:37 AM, Cameron Simpson <cs at zip.com.au> wrote:
> There are two approaches to this.
> You can create a file while your umask is 0777... [or]
> My personal habit is to make a directory for the lock

Both viable options; I'd be inclined toward the second. Or, here's a
third option. Instead of writing to a shared network drive, submit
requests on a TCP socket direct to the monitor program. Spin off a
thread that does this:

* Wait for incoming socket connection
* Set overall cutoff timer; if (say) 2 seconds pass, kill the connection
* Authenticate the client (if applicable)
* Accept the update data, sanitize if necessary
* Write the file to disk
* Notify the XManager
* Loop.

Do all this on *one thread* and then you eliminate all race
conditions. Good use of a TCP listen queue and the cutoff timer will
mean that applications aren't actually kept waiting, but they're still
rigidly locked into a queue - depending on how frequent your updates
are, this could be a problem. If you need simultaneous updates, spin
off a new thread for each socket connection, and then use something
simple like a mapping of file name to semaphore to ensure no two try
to update the same file at once.

By moving the actual file read/writes to a single computer, you
simplify the task of notifying the parent. In fact, if there's only
one process that needs to be made aware of the change, the job's even
easier - all you need to do is change a variable or call a method or
whatever it be, right there in the socket handler.

Chris Angelico

More information about the Python-list mailing list