TaurusEmitterThread
¶
digraph inheritance6ea44c0ffd {
rankdir=UD;
ratio=compress;
size="8.0, 12.0";
"QObject" [color=dodgerblue1,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded];
"wrapper" -> "QObject" [arrowsize=0.5,style="setlinewidth(0.5)"];
"QThread" [color=dodgerblue1,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded];
"QObject" -> "QThread" [arrowsize=0.5,style="setlinewidth(0.5)"];
"TaurusEmitterThread" [URL="#taurus.qt.qtcore.util.emitter.TaurusEmitterThread",color=dodgerblue1,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,target="_top",tooltip="This object get items from a python Queue and performs a thread safe "];
"QThread" -> "TaurusEmitterThread" [arrowsize=0.5,style="setlinewidth(0.5)"];
"simplewrapper" [color=dodgerblue1,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded];
"wrapper" [color=dodgerblue1,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded];
"simplewrapper" -> "wrapper" [arrowsize=0.5,style="setlinewidth(0.5)"];
}
-
class
TaurusEmitterThread
(parent=None, name='', queue=None, method=None, cursor=None, sleep=5000, polling=0, loopwait=5)[source]¶ Bases:
PyQt4.QtCore.QThread
This object get items from a python Queue and performs a thread safe operation on them. It is useful to serialize Qt tasks in a background thread.
Parameters: - parent – a Qt/Taurus object
- name – identifies object logs
- queue – if None parent.getQueue() is used, if not then the queue passed as argument is used
- method – the method to be executed using each queue item as argument
- cursor – if True or QCursor a custom cursor is set while the Queue is not empty
How TaurusEmitterThread works:
TaurusEmitterThread is a worker thread that processes a queue of iterables passed as arguments to the specified method every time that
doSomething()
is called:self.method(*item)
will be called if TaurusEmitterThread.method- has been initialized.
item[0](item[1:])
will be called ifmethod
is not initialized- and the first element of the queued iterable is callable.
TaurusEmitterThread uses two queues:
self.queue
manages the objects added externally:
- the
next()
method passes objects fromself.queue
toself.todo queue
- every time that a somethingDone signal arrives
next()
is called. next()
can be called also externally to ensure that the main queue is being processed.- the queue can be accessed externally using
getQueue()
getQueue().qsize()
returns number of remaining objects in queue.- while there are objects in queue the
.next()
method will override applications cursor. a call tonext()
with an empty queue will restore the original cursor.
self.todo
is managed by therun()/start()
method:
- a loop waits continuously for new objects in
self.todo
queue. - if an object is found, it is sent in a doSomething signal.
- if “exit” is found the loop exits.
Usage example:
#Applying TaurusEmitterThread to an existing class: from queue import Queue from functools import partial def modelSetter(args): obj,model = args[0],args[1] obj.setModel(model) klass TaurusGrid(Qt.QFrame, TaurusBaseWidget): ... def __init__(self, parent = None, designMode = False): ... self.modelsQueue = Queue() self.modelsThread = TaurusEmitterThread(parent=self, queue=self.modelsQueue,method=modelSetter ) ... def build_widgets(...): ... previous,synoptic_value = synoptic_value,TaurusValue(cell_frame) #synoptic_value.setModel(model) self.modelsQueue.put((synoptic_value,model)) ... def setModel(self,model): ... if hasattr(self,'modelsThread') and not self.modelsThread.isRunning(): self.modelsThread.start() elif self.modelsQueue.qsize(): self.modelsThread.next() ...