message_loader_thread.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import threading
34 
35 
36 class MessageLoaderThread(threading.Thread):
37  """
38  Waits for a new playhead position on the given topic, then loads the message at that position and notifies the view threads.
39 
40  One thread per topic. Maintains a cache of recently loaded messages.
41  """
42  def __init__(self, timeline, topic):
43  threading.Thread.__init__(self)
44 
45  self.timeline = timeline
46  self.topic = topic
47 
49 
51  self._message_cache = {}
53 
54  self._stop_flag = False
55 
56  self.setDaemon(True)
57  self.start()
58 
59  def reset(self):
61 
62  def run(self):
63  while not self._stop_flag:
64  # Wait for a new entry
65  cv = self.timeline._playhead_positions_cvs[self.topic]
66  with cv:
67  while (self.topic not in self.timeline._playhead_positions) or (self.topic_playhead_position == self.timeline._playhead_positions[self.topic]):
68  cv.wait()
69  if self._stop_flag:
70  return
71  playhead_position = self.timeline._playhead_positions[self.topic]
72 
73  self.topic_playhead_position = playhead_position
74 
75  # Don't bother loading the message if there are no listeners
76  if not self.timeline.has_listeners(self.topic):
77  continue
78 
79  # Load the message
80  if playhead_position is None:
81  msg_data = None
82  else:
83  msg_data = self._get_message(playhead_position)
84 
85  # Inform the views
86  messages_cv = self.timeline._messages_cvs[self.topic]
87  with messages_cv:
88  self.timeline._messages[self.topic] = msg_data
89  messages_cv.notify_all() # notify all views that a message is loaded
90 
91  def _get_message(self, position):
92  key = str(position)
93  if key in self._message_cache:
94  return self._message_cache[key]
95 
96  msg_data = self.timeline.read_message(self.topic, position)
97 
98  self._message_cache[key] = msg_data
99  self._message_cache_keys.append(key)
100 
101  if len(self._message_cache) > self._message_cache_capacity:
102  oldest_key = self._message_cache_keys[0]
103  del self._message_cache[oldest_key]
104  self._message_cache_keys.remove(oldest_key)
105 
106  return msg_data
107 
108  def stop(self):
109  self._stop_flag = True
110  cv = self.timeline._playhead_positions_cvs[self.topic]
111  with cv:
112  cv.notify_all()


rqt_py_trees
Author(s): Thibault Kruse, Michal Staniaszek, Daniel Stonier, Naveed Usmani
autogenerated on Mon Jun 10 2019 14:55:56