LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Public Attributes | Private Attributes | List of all members
lsst.utils.multithreading.SharedData.SharedData Class Reference
Inheritance diagram for lsst.utils.multithreading.SharedData.SharedData:

Public Member Functions

def __init__
 
def __enter__
 
def __exit__
 
def __getattribute__
 
def __setattr__
 
def initData
 
def dir
 

Public Attributes

 acquire
 
 release
 
 notify
 
 notifyAll
 
 wait
 

Private Attributes

 _d
 
 _cond
 
 _is_owned
 
 _lockOnRead
 

Detailed Description

a lock-protected container for data that can be shared amongst threads.

This container holds data that is intended to be shared amongst multiple
threads.  In order to update or (optionally) examine the data, one must
first aquire the lock associated with the container.

This container also behaves like a threading.Container, via its 
wait(), notify(), and notifyAll() functions.  Also like Condition, 
acquire() is reentrant.  

SharedData instances may be used with the with statement:

  sd = SharedData()
  with sd:
      sd.blah = 1

The with statement will acquire the lock and ensure that it is released
when its block is exited.  

Definition at line 27 of file SharedData.py.

Constructor & Destructor Documentation

def lsst.utils.multithreading.SharedData.SharedData.__init__ (   self,
  needLockOnRead = True,
  data = None,
  cond = None 
)
create and initialize the shared data
@param needLockOnRead   if true (default), acquiring the lock will 
      be needed when reading the data.  This is recommended
      if the data items are anything but primitive types;
      otherwise, a compound data item (e.g. of type dict)
      could be updated without acquiring a lock.  
@param data   a dictionary of data to initialize the container with.
      This is done by calling initData().  Set this value to
      False when calling from a subclass constructor; this
      will allow any non-protected attributes to be set via
      the subclass's constructor.  If None is given (default),
      it is assumed that all new attributes will be considered
      protected data.
@param cond   Reuse this existing Condition instance to protect this
        container

Definition at line 49 of file SharedData.py.

49 
50  def __init__(self, needLockOnRead=True, data=None, cond=None):
51  """
52  create and initialize the shared data
53  @param needLockOnRead if true (default), acquiring the lock will
54  be needed when reading the data. This is recommended
55  if the data items are anything but primitive types;
56  otherwise, a compound data item (e.g. of type dict)
57  could be updated without acquiring a lock.
58  @param data a dictionary of data to initialize the container with.
59  This is done by calling initData(). Set this value to
60  False when calling from a subclass constructor; this
61  will allow any non-protected attributes to be set via
62  the subclass's constructor. If None is given (default),
63  it is assumed that all new attributes will be considered
64  protected data.
65  @param cond Reuse this existing Condition instance to protect this
66  container
67  """
68  self._d = {}
69  if cond is None:
70  cond = threading.Condition()
71  self._cond = cond
72 
73  # behave like a Condition
74  self.acquire = cond.acquire
75  self.release = cond.release
76  self.notify = cond.notify
77  self.notifyAll = cond.notifyAll
78  self.wait = cond.wait
79  self._is_owned = cond._is_owned
80 
81  self._lockOnRead = needLockOnRead
82 
83  if isinstance(data, dict):
84  self.initData(data)
85  if data is None:
86  self._d["__"] = True

Member Function Documentation

def lsst.utils.multithreading.SharedData.SharedData.__enter__ (   self)

Definition at line 88 of file SharedData.py.

88 
def __enter__(self): return self._cond.__enter__();
def lsst.utils.multithreading.SharedData.SharedData.__exit__ (   self,
  exc_info 
)

Definition at line 89 of file SharedData.py.

89 
90  def __exit__(self, *exc_info): return self._cond.__exit__(*exc_info);
def lsst.utils.multithreading.SharedData.SharedData.__getattribute__ (   self,
  name 
)

Definition at line 91 of file SharedData.py.

91 
92  def __getattribute__(self, name):
93  if name == "_d" or len(self._d) == 0 or not self._d.has_key(name):
94  return object.__getattribute__(self, name)
95 
96  if self._lockOnRead and not self._is_owned():
97  raise AttributeError("%s: lock required for read access" % name)
98  return self._d[name]
99 
def lsst.utils.multithreading.SharedData.SharedData.__setattr__ (   self,
  name,
  value 
)

Definition at line 100 of file SharedData.py.

101  def __setattr__(self, name, value):
102  if name == "_d" or len(self._d) == 0 or name in self.__dict__.keys():
103  object.__setattr__(self, name, value)
104  return
105 
106  if not self._is_owned():
107  raise AttributeError("%s: lock required for write access" % name)
108 
109  self._d[name] = value
110 
def lsst.utils.multithreading.SharedData.SharedData.dir (   self)

Definition at line 139 of file SharedData.py.

140  def dir(self):
141  return filter(lambda k: k != "__", self._d.keys())
142 
def lsst.utils.multithreading.SharedData.SharedData.initData (   self,
  data 
)
initialize the container with the data from a dictionary.  
@param data   a dictionary of data to initialize the container with.
      Attributes will be added to this container with names
      matching the given the dictionary's key names and
      initialized to their corresponding values.  The keys
      cannot match an existing function (or internal attribute)
      name.
@throws ValueError   if the dictionary has a key that conflicts with
      an existing function or internal attribute name.  

Definition at line 111 of file SharedData.py.

112  def initData(self, data):
113  """
114  initialize the container with the data from a dictionary.
115  @param data a dictionary of data to initialize the container with.
116  Attributes will be added to this container with names
117  matching the given the dictionary's key names and
118  initialized to their corresponding values. The keys
119  cannot match an existing function (or internal attribute)
120  name.
121  @throws ValueError if the dictionary has a key that conflicts with
122  an existing function or internal attribute name.
123  """
124  with self._cond:
125  bad = []
126  realattrs = self.__dict__.keys()
127  for key in data.keys():
128  if key in realattrs:
129  bad.append(key)
130  if len(bad) > 0:
131  raise ValueError("Names cause conflicts with functions or " +
132  "internal data: " + str(bad))
133 
134  for key in data.keys():
135  self._d[key] = data[key]
136 
137  if len(self._d) == 0:
138  self._d["__"] = True

Member Data Documentation

lsst.utils.multithreading.SharedData.SharedData._cond
private

Definition at line 70 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData._d
private

Definition at line 67 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData._is_owned
private

Definition at line 78 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData._lockOnRead
private

Definition at line 80 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData.acquire

Definition at line 73 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData.notify

Definition at line 75 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData.notifyAll

Definition at line 76 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData.release

Definition at line 74 of file SharedData.py.

lsst.utils.multithreading.SharedData.SharedData.wait

Definition at line 77 of file SharedData.py.


The documentation for this class was generated from the following file: