class SignalThread(QThread): def __init__(self, parent = None): QThread.__init__(self, parent) self.waitcond = QWaitCondition() self.mutex = QMutex() self.isstopped = False def trigger(self): """lock first to make sure the QThread is actually waiting for a signal""" self.mutex.lock() self.waitcond.wakeOne() self.mutex.unlock() def stopThread(self): self.mutex.lock() self.isstopped = True self.waitcond.wakeOne() self.mutex.unlock() def run(self): self.mutex.lock() while not self.isstopped: # just wait, and trigger every time we receive a signal self.waitcond.wait(self.mutex) if not self.isstopped: self.emit(SIGNAL("triggerSignal()")) self.mutex.unlock()
class SignalThread(QThread): def __init__(self, parent=None): QThread.__init__(self, parent) self.waitcond = QWaitCondition() self.mutex = QMutex() self.isstopped = False def trigger(self): """lock first to make sure the QThread is actually waiting for a signal""" self.mutex.lock() self.waitcond.wakeOne() self.mutex.unlock() def stopThread(self): self.mutex.lock() self.isstopped = True self.waitcond.wakeOne() self.mutex.unlock() def run(self): self.mutex.lock() while not self.isstopped: # just wait, and trigger every time we receive a signal self.waitcond.wait(self.mutex) if not self.isstopped: self.emit(SIGNAL("triggerSignal()")) self.mutex.unlock()
class WorkerThread(QThread): ''' Schedule things to do Wait all time or launch/wake other threads to recompute max amount of node to display with or without zoom, refresh/redraw paint area, etc. . ''' def __init__(self, parent = None): super(WorkerThread, self).__init__(parent) self.mutex = QMutex() self.condition = QWaitCondition() self.restart = False self.abort = False self.timeline = parent self.countThread = parent.countThread self.populateThread = parent.populateThread self.maxOccThread = parent.maxOccThread def __del__(self): self.mutex.lock() self.abort = True self.condition.wakeOne() self.mutex.unlock() self.wait() def render(self): locker = QMutexLocker(self.mutex) if not self.isRunning() and not self.countThread.isRunning() and not self.populateThread.isRunning() and not self.maxOccThread.isRunning(): self.start(QThread.LowPriority) else: self.restart = True self.condition.wakeOne() def run(self): while True: self.mutex.lock() # Fetch value from timeline nodeCount = self.timeline.nodeCount dataListsCreated = self.timeline.dataListsCreated if dataListsCreated: self.timeline.findMaxValue() xHop = self.timeline.xHop maxOcc = self.timeline.maxOcc self.mutex.unlock() if not nodeCount and not self.restart and not self.countThread.isRunning(): self.countThread.start() elif nodeCount and not dataListsCreated and not self.restart and not self.populateThread.isRunning(): self.populateThread.start() elif nodeCount and dataListsCreated and xHop and not maxOcc and not self.restart and not self.maxOccThread.isRunning(): self.maxOccThread.start() elif nodeCount and dataListsCreated and xHop and maxOcc and not self.restart and not self.maxOccThread.isRunning(): self.emit(SIGNAL('refresh'), True) if self.abort: return self.mutex.lock() if not self.restart: self.condition.wait(self.mutex) self.restart = False self.mutex.unlock()
class ListThread(QThread): def __init__(self, ListView, ListModel): QThread.__init__(self) self.mutex = QMutex() self.condition = QWaitCondition() self.ListView = ListView self.ListModel = ListModel self.restart = False self.abort = False def __del__(self): self.mutex.lock() self.abort = True self.condition.wakeOne() self.mutex.unlock() self.wait() def renderList(self, list, nodeParent): locker = QMutexLocker(self.mutex) self.list = list self.nodeParent = nodeParent if not self.isRunning(): self.start(QThread.LowPriority) else: self.restart = True self.condition.wakeOne() def run(self): while(True): self.mutex.lock() self.currentNodeDir = self.nodeParent list = self.list self.mutex.unlock() for itemVFS in list: items = [] if self.restart: break if self.abort: return items.append(NodeItem(itemVFS)) items[0].setText(QApplication.translate("MainWindow", str(itemVFS.name), None, QApplication.UnicodeUTF8)) if not itemVFS.next.empty() or not itemVFS.is_file: #XXX if not itemVFS.is_file : icon = ":folder.png" else : icon = ":folder.png" # items[0].setIcon(QIcon(":dff_partition.png")) else : icon = ":file.png" items.append(QStandardItem()) if itemVFS.is_file == 0 : items[1].setText(QApplication.translate("ListModel", "", None, QApplication.UnicodeUTF8)) else : items[1].setText(DFF_Utils.formatSize(itemVFS.attr.size)) items.append(QStandardItem("")) items.append(QStandardItem("")) items.append(QStandardItem("")) items.append(QStandardItem("")) time = itemVFS.attr.time for i in time : items[self.ListModel.timeHeader[str(i)]].setText(str(time[i].get_time())) items[5].setText(itemVFS.fsobj.name) for i in range(0, 6): items[i].setEditable(False) items[i].setCheckable(False) self.emit(SIGNAL("addItem"), items, icon) sleep(0.001) if not self.restart: self.emit(SIGNAL("resizeList")) self.mutex.lock() if not self.restart: self.condition.wait(self.mutex) self.restart = False self.mutex.unlock()
class Queue(object): """Create a queue object with a given maximum size. """ def __init__(self, maxsize=0): self.maxsize = maxsize self.queue = deque() # Mutex using for accessing the deque self.mutex = QMutex() # Condition that will be held when the queue is empty and the consumer # needs to wait for a new item self.item_added = QWaitCondition() # Condition that will be held when the queue is full and the producer # needs to wait for a new place to insert the item self.item_removed = QWaitCondition() def put(self, item, block=True, timeout=None): """Put an item into the queue. Parameters ---------- block : bool If True(default), the caller thread will block until the queue has a free space available for putting an new item. If False, the `Full` exception will be raised if there is no free space in the queue timeout : int The max time to wait for a new space to be avaible, in milliseconds. """ self.mutex.lock() try: # Check if the queue has a limit (0 means not) if self.maxsize > 0: # Raise Full if block is False and the queue is at max cap. if not block: if self._qsize() == self.maxsize: raise Full # If a timeout is not provided, wait indefinitely elif timeout is None: while self._qsize() == self.maxsize: self.item_removed.wait(self.mutex) elif timeout < 0: raise ValueError("'timeout' must be a non-negative number") else: timer = QElapsedTimer() timer.start() while self._qsize() == self.maxsize: remaining = timeout - timer.elapsed() if remaining <= 0.0: raise Full self.item_removed.wait(self.mutex, remaining) self._put(item) self.item_added.wakeOne() finally: self.mutex.unlock() def get(self, block=True, timeout=None): """Remove and return an item from the queue. Parameters ---------- block : bool If True(default), the caller thread will block until the queue has an item available for putting an new item. If False, the `Empty` exception will be raised if there is no item in the queue timeout : int The max time to wait for a new item to be avaible, in milliseconds. """ self.mutex.lock() try: if not block: if not self._qsize(): raise Empty elif timeout is None: while not self._qsize(): self.item_added.wait(self.mutex) elif timeout < 0: raise ValueError("'timeout' must be a non-negative number") else: timer = QElapsedTimer() timer.start() while not self._qsize(): remaining = timeout - timer.elapsed() if remaining <= 0.0: raise Empty self.item_added.wait(self.mutex, remaining) item = self._get() self.item_removed.wakeOne() return item finally: self.mutex.unlock() def _qsize(self, len=len): return len(self.queue) # Put a new item in the queue def _put(self, item): self.queue.append(item) # Get an item from the queue def _get(self): return self.queue.popleft() def _clear(self): self.queue.clear()
class LaberintoThread(QThread): laberintoTerminado = pyqtSignal(bool) def __init__(self, parent=None): super().__init__() self.laberinto = Laberinto(parent=self) self.mutex = QMutex() self.waitWorker = QWaitCondition() self.waitController = QWaitCondition() self.parent = parent self.running = True self.solving = False self.timer = QTimer() self.delay = 20 self.timer.timeout.connect(self.continue_) self.laberintoTerminado.connect(self.handleLaberintoTerminado) self.start() def __del__(self): self.running = False self.waitWorker.wakeOne() self.wait() ##### interfaz que sobreescribo (métodos públicos) def tamano(self, *args): return self.laberinto.tamano(*args) def cargar(self, *args): self.laberinto.cargar(*args) def resolver(self): self.mutex.lock() if not self.solving: self.waitWorker.wakeOne() self.mutex.unlock() def resetear(self, *args): return self.laberinto.resetear(*args) def esPosicionRata(self, *args): return self.laberinto.esPosicionRata(*args) def esPosicionQueso(self, *args): return self.laberinto.esPosicionQueso(*args) def get(self, *args): return self.laberinto.get(*args) def getInfoCelda(self, *args): return self.laberinto.getInfoCelda(*args) def getPosicionRata(self, *args): return self.laberinto.getPosicionRata(*args) def getPosicionQueso(self, *args): return self.laberinto.getPosicionQueso(*args) def setPosicionRata(self, *args): return self.laberinto.setPosicionRata(*args) def setPosicionQueso(self, *args): return self.laberinto.setPosicionQueso(*args) ##### intefaz nueva def update(self): self.mutex.unlock() self.parent.update() self.mutex.lock() self.timer.stop() self.timer.start(self.delay) self.waitController.wait(self.mutex) def continue_(self): self.mutex.lock() self.timer.stop() self.waitController.wakeOne() self.mutex.unlock() def setAnimationDelay(self, delay): self.mutex.lock() self.delay = delay self.mutex.unlock() def handleLaberintoTerminado(self, e): if e: self.parent.laberintoResuelto() else: self.parent.laberintoIncompleto() def lock(self): self.mutex.lock() def unlock(self): self.mutex.unlock() ##### main del thread def run(self): while self.running: self.mutex.lock() self.waitWorker.wait(self.mutex) if self.running: self.solving = True self.res = self.laberinto.resolver() self.laberintoTerminado.emit(self.res) self.solving = False self.mutex.unlock()