Watchpoint
Overview
Watchpoints are directories created for communication state machine states between independent processes on a system. These independent processes can "watch" for these directories to be created in to determine when another process has started or completed an action. This provides a handy way to synchronize processes that otherwise may have difficulty communicating with one another.
Why directories?
On Linux, directory creation is an atomic process, eliminating race conditions and being thread safe (unlike file creation).
Usage
The module is broken up into two main components: Watchpoint
and WatchpointHandler
objects.
The handler contains a defined list of watchpoints that will be valid for the current session.
Each Watchpoint
has a base path in which it will be created, as well as a key and/or name.
Specifying a name allows the key
property to be either a str
or an int
, in case an enum
is desired for keying a long list of watchpoints.
Otherwise, the name of a watchpoint is its key and both are of str
type.
# Example implementation
>>> l_example = [Watchpoint('WP-A', '.'), Watchpoint(42, 'test/subdir', 'IAmNamed')]
>>> handler = WatchpointHandler(l_example)
>>> handler.create('WP-A')
>>> handler.create(42)
>>> handler.exists('WP-A')
True
>>> handler.exists(42)
True
The above code yields a directory structure like:
<cwd>/
|
*-->WP-A.watch/
|
*-->test/
|
*-->subdir/
|
*-->IAmNamed.watch/
Removing the watchpoints deletes their directory but not their parent directory:
# Remove the named watchpoint
>>> handler.remove(42)
Yields:
<cwd>/
|
*-->WP-A.watch/
|
*-->test/
|
*-->subdir/
Watchpoint Lists and Dynamic Creation
Dynamic watchpoint creation is not supported by WatchpointHandler
because this module operates
on the premise that independent scripts or processes are using directory creation for
synchronization.
In this case, both scripts know of a set or subset of watchpoint directories.
Adding more Watchpoints into the WatchpointHandler
in Script A would not load them into
Script B's handler (and vice-versa), so there is little point to them.
Testing
Testing is conducted through pytest
.
This can be installed for local verification via pip install --user pytest
.