CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ThreadChain.py
Go to the documentation of this file.
1 from PyQt4.QtCore import QThread,SIGNAL
2 
3 import logging
4 
5 class ThreadChain(QThread):
6  """ Holds a list of commands that shall be executed in one Thread in a chain.
7 
8  The chain can run in two modes: Eighter it receives a command with optional attributes on construction. The return value can be accessed by calling returnValue() without arguments.
9  In the second mode the constructor does not receive any arguments and commands are passed to the chain with addComand().
10  This function returns an id unique for the command making the return value of the command available through retrunValue(id).
11  Start the ThreadChain using start().
12 
13  One can check if the thread is still running using isRunning().
14  When all commands are executed a signal "finishedThreadChain" will be emitted.
15  """
16  NO_THREADS_FLAG=False
17  def __init__(self, command=None, *attr):
18  QThread.__init__(self, None)
19  self._commandTuples = []
20  self._commandCounter = -1
21  self._returnValues = {}
22  if command:
23  self.addCommand(command, *attr)
24  self.start()
25 
26  def addCommand(self, command, *attr):
27  """ Adds a command to this ThreadChain
28 
29  and returns an id which is required to obtain the return value of this command.
30 
31  *attr is a optional tuple of arguments which will be passed to the command on execution.
32  """
33  self._commandCounter += 1
34  id = self._commandCounter
35  self._commandTuples += [(id, command, attr)]
36  if self.NO_THREADS_FLAG:
37  self._returnValues[id] = command.__call__(*attr)
38  self.emit(SIGNAL('finishedThreadChain'), self._returnValues.values())
39  return
40 
41  def clearReturnValues(self):
42  self._returnValues.clear()
43 
44  def clearReturnValue(self, command):
45  if command in self._returnValues.keys():
46  self._returnValues.pop(command)
47  return True
48  return False
49 
50  def returnValue(self, id=None):
51  """ Returns return value of command with given id.
52 
53  The id is returned by addCommand().
54  If id is None the return value of the last command will be returned.
55  """
56  if id in self._returnValues.keys():
57  return self._returnValues[id]
58  valueLength = len(self._returnValues)
59  if valueLength == 0:
60  return []
61  # TODO: maybe raise exception to distinguish from None return value?
62  return self._returnValues[self._returnValues.keys()[valueLength-1]]
63 
64  def start(self):
65  if self.NO_THREADS_FLAG:
66  return
67  if not self.isRunning():
68  QThread.start(self)
69 
70  def run(self):
71  if self.NO_THREADS_FLAG:
72  return
73  while self._commandTuples != []:
74  id, command, attr = self._commandTuples.pop(0)
75  self._returnValues[id] = command.__call__(*attr)
76 
77  # signal contains list of return values
78  # for compatibility with old implementation
79  self.emit(SIGNAL('finishedThreadChain'), self._returnValues.values())