class Task(object): def __init__(self, request): self._request = request self._cond = QWaitCondition() def getRequest(self): return self._request def waitForResponse(self): self._cond.wait(QMutex()) x = self._response del self._response return x def addResponse(self, response): # Don't allow two responses to the same # request. Could possibly happen # if someone clicks accept/reject and simultaneously # closes the window, somehow, triggering # the closeEvent if self._cond: self._response = response self._cond.wakeAll() self._cond = None def setType(self, dialogType): """ Set what type of task this is. The dialogtype is the construtor for the Dialog that is to be invoked, when onSystrayActivated is invoked later on""" self.dialogType = dialogType def getType(self): return self.dialogType
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 __init__(self, searcher, parent): QThread.__init__(self, parent) self._searcher = searcher self._quit = False self._mutex = QMutex() self._pending = QWaitCondition() self._collector = None self._query = None self._result = None
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 __init__( self, plugin, parent = None ): QThread.__init__( self, parent ) self.__plugin = plugin self.__requestQueue = deque() self.__stopRequest = False self.__lock = QMutex() self.__condition = QWaitCondition() return
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()
class VCSPluginThread( QThread ): " Wrapper for the plugin thread " def __init__( self, plugin, parent = None ): QThread.__init__( self, parent ) self.__plugin = plugin self.__requestQueue = deque() self.__stopRequest = False self.__lock = QMutex() self.__condition = QWaitCondition() return def run( self ): " Thread loop " while not self.__stopRequest: self.__lock.lock() while self.__requestQueue: path, flag = self.__requestQueue.pop() self.__lock.unlock() time.sleep( 0.01 ) self.__processRequest( path, flag ) if self.__stopRequest: break self.__lock.lock() if self.__stopRequest: self.__lock.unlock() break self.__condition.wait( self.__lock ) self.__lock.unlock() return def __processRequest( self, path, flag ): " Processes a single request. It must be exception safe. " try: statuses = self.__plugin.getObject().getStatus( path, flag ) for status in statuses: if len( status ) == 3: self.emit( SIGNAL( "VCSStatus" ), path + status[ 0 ], status[ 1 ], status[ 2 ] ) else: self.emit( SIGNAL( "VCSStatus" ), path, IND_VCS_ERROR, "The " + self.__plugin.getName() + " plugin " "does not follow the getStatus() interface " "agreement" ) except Exception, exc: self.emit( SIGNAL( "VCSStatus" ), path, IND_VCS_ERROR, "Exception in " + self.__plugin.getName() + " plugin while retrieving VCS status: " + str( exc ) ) except:
def __init__(self, interval, projectDir, vcs, parent = None): """ Constructor @param interval new interval in seconds (integer) @param projectDir project directory to monitor (string or QString) @param vcs reference to the version control object @param parent reference to the parent object (QObject) """ QThread.__init__(self, parent) self.setObjectName("VcsStatusMonitorThread") self.setTerminationEnabled(True) self.projectDir = QString(projectDir) self.vcs = vcs self.interval = interval self.autoUpdate = False self.statusList = QStringList() self.reportedStates = {} self.shouldUpdate = False self.monitorMutex = QMutex() self.monitorCondition = QWaitCondition() self.__stopIt = False
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 __init__(self, parent): QThread.__init__(self, parent) self.classObjectLock = QMutex(QMutex.Recursive) self.classParamDict = {} self.classInstanceDict = {} self.queueLock = QMutex(QMutex.Recursive) self.queueHasJobsLock = QMutex() # lock lighter than queueLock # while waiting for this lock no # lock on the latter can be hold self.queueHasJobsCondition = QWaitCondition() self.renderQueue = [] # contains all render requests self.newestId = 0 # newest job Id self.renderingLock = QMutex(QMutex.Recursive) self.renderingFinishedLock = QMutex() # lock lighter than renderingLock # while waiting for this lock no # lock on the latter can be hold self.renderingFinishedCondition = QWaitCondition() self.currentlyRenderingJob = None
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()
def __init__(self, suite, items, stopOnError, stopOnFail, xmlRPCUrl): QThread.__init__(self) self.suite = suite self.items = items executionDelay = self.suite.sliderSpeed.value() / 1000 # convert to seconds self.sync = QMutex() self.pauseCond = QWaitCondition() self.executionPaused = False self.autoPlayer = AutoPlayer(executionDelay, self.sync, self.pauseCond, xmlRPCUrl) self.executionStopped = False self.stopOnError = stopOnError self.stopOnFail = stopOnFail self._connectSignals()
class VcsStatusMonitorThread(QThread): """ Class implementing the VCS status monitor thread base class. @signal vcsStatusMonitorData(QStringList) emitted to update the VCS status @signal vcsStatusMonitorStatus(QString, QString) emitted to signal the status of the monitoring thread (ok, nok, op) and a status message """ def __init__(self, interval, projectDir, vcs, parent = None): """ Constructor @param interval new interval in seconds (integer) @param projectDir project directory to monitor (string or QString) @param vcs reference to the version control object @param parent reference to the parent object (QObject) """ QThread.__init__(self, parent) self.setObjectName("VcsStatusMonitorThread") self.setTerminationEnabled(True) self.projectDir = QString(projectDir) self.vcs = vcs self.interval = interval self.autoUpdate = False self.statusList = QStringList() self.reportedStates = {} self.shouldUpdate = False self.monitorMutex = QMutex() self.monitorCondition = QWaitCondition() self.__stopIt = False def run(self): """ Protected method implementing the tasks action. """ while not self.__stopIt: # perform the checking task self.statusList.clear() self.emit(SIGNAL("vcsStatusMonitorStatus(QString, QString)"), QString("wait"), self.trUtf8("Waiting for lock")) try: locked = self.vcs.vcsExecutionMutex.tryLock(5000) except TypeError: locked = self.vcs.vcsExecutionMutex.tryLock() if locked: try: self.emit(SIGNAL("vcsStatusMonitorStatus(QString, QString)"), QString("op"), self.trUtf8("Checking repository status")) res, statusMsg = self._performMonitor() finally: self.vcs.vcsExecutionMutex.unlock() if res: status = QString("ok") else: status = QString("nok") self.emit(SIGNAL("vcsStatusMonitorStatus(QString, QString)"), QString("send"), self.trUtf8("Sending data")) self.emit(SIGNAL("vcsStatusMonitorData(QStringList)"), QStringList(self.statusList)) self.emit(SIGNAL("vcsStatusMonitorStatus(QString, QString)"), status, statusMsg) else: self.emit(SIGNAL("vcsStatusMonitorStatus(QString, QString)"), QString("timeout"), self.trUtf8("Timed out waiting for lock")) if self.autoUpdate and self.shouldUpdate: try: self.vcs.vcsUpdate(self.projectDir, True) continue # check again except TypeError: pass # compatibility for older VCS plugins self.shouldUpdate = False # wait until interval has expired checking for a stop condition self.monitorMutex.lock() if not self.__stopIt: self.monitorCondition.wait(self.monitorMutex, self.interval * 1000) self.monitorMutex.unlock() self.exit() def setInterval(self, interval): """ Public method to change the monitor interval. @param interval new interval in seconds (integer) """ locked = self.monitorMutex.tryLock() self.interval = interval self.monitorCondition.wakeAll() if locked: self.monitorMutex.unlock() def getInterval(self): """ Public method to get the monitor interval. @return interval in seconds (integer) """ return self.interval def setAutoUpdate(self, auto): """ Public method to enable the auto update function. @param auto status of the auto update function (boolean) """ self.autoUpdate = auto def getAutoUpdate(self): """ Public method to retrieve the status of the auto update function. @return status of the auto update function (boolean) """ return self.autoUpdate def checkStatus(self): """ Public method to wake up the status monitor thread. """ locked = self.monitorMutex.tryLock() self.monitorCondition.wakeAll() if locked: self.monitorMutex.unlock() def stop(self): """ Public method to stop the monitor thread. """ locked = self.monitorMutex.tryLock() self.__stopIt = True self.monitorCondition.wakeAll() if locked: self.monitorMutex.unlock() def clearCachedState(self, name): """ Public method to clear the cached VCS state of a file/directory. @param name name of the entry to be cleared (QString or string) """ project = e4App().getObject("Project") key = project.getRelativePath(unicode(name)) try: del self.reportedStates[key] except KeyError: pass def _performMonitor(self): """ Protected method implementing the real monitoring action. This method must be overridden and populate the statusList member variable with a list of strings giving the status in the first column and the path relative to the project directory starting with the third column. The allowed status flags are: <ul> <li>"A" path was added but not yet comitted</li> <li>"M" path has local changes</li> <li>"O" path was removed</li> <li>"R" path was deleted and then re-added</li> <li>"U" path needs an update</li> <li>"Z" path contains a conflict</li> <li>" " path is back at normal</li> </ul> @return tuple of flag indicating successful operation (boolean) and a status message in case of non successful operation (QString) """ raise RuntimeError('Not implemented')
import urllib from bs4 import BeautifulSoup import os from PyQt4.QtCore import (QThread, QDir, QMutex, QWaitCondition, QSettings, QVariant, QStringList) from PyQt4.QtGui import (QMainWindow, QMessageBox, QImage, QPixmap, QKeySequence, QDialog, QDialogButtonBox, QAction) import datetime from Gui import MangaDownloader, Dialog_Settings from PyQt4.QtCore import pyqtSignal as Signal import resource import tempfile import zipfile import re # Elements for waiting user confirmation for overriding file waitCondition = QWaitCondition() mutex = QMutex() class MainWindow(QMainWindow, MangaDownloader.Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) # self.mutex = QMutex() self.settings_dict = dict(recent=5, save_dir=QDir.homePath(), close_confirmation=True, display_image=True, list_recent=[]) self.pushbutton_stop.setEnabled(False) self.edit_url.setFocus() self.image = QImage() self.pushbutton_start.clicked.connect(self.startDownload) self.action_Quit.setShortcut(QKeySequence.Quit) self.action_Quit.triggered.connect(self.close)
def __init__(self, request): self._request = request self._cond = QWaitCondition()
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()
class _FTSearchThread(QThread): '''This thread performs full text search in the background''' searchFinished = pyqtSignal() searchError = pyqtSignal() def __init__(self, searcher, parent): QThread.__init__(self, parent) self._searcher = searcher self._quit = False self._mutex = QMutex() self._pending = QWaitCondition() self._collector = None self._query = None self._result = None def run(self): while not self._quit: self._mutex.lock() if not self._query: self._pending.wait(self._mutex) query = self._query self._query = None self._mutex.unlock() # search if query: (query_str1, query_str2, itemtypes, limit, highlight, merge) = query self._mutex.lock() collector = self._searcher.make_collector(limit) self._collector = collector self._mutex.unlock() try: result = self._searcher.search( collector, query_str1, query_str2, itemtypes, highlight) except: self._mutex.lock() self._result = None self._mutex.unlock() self.searchError.emit() else: if collector.aborted: pass else: self._mutex.lock() self._result = (merge, result) self._mutex.unlock() self.searchFinished.emit() self._mutex.lock() self._collector = None self._mutex.unlock() def cancel(self): self._mutex.lock() self._query = None if self._collector: self._collector.abort() self._mutex.unlock() def quit(self): self._mutex.lock() if self._collector: self._collector.abort() self._query = None self._quit = True self._mutex.unlock() self._pending.wakeAll() def update_query(self, query_str1=None, query_str2=None, itemtypes=(), limit=1000, highlight=False, merge=False): self._mutex.lock() if self._collector: self._collector.abort() self._query = (query_str1, query_str2, itemtypes, limit, highlight, merge) self._mutex.unlock() self._pending.wakeAll() def take_result(self): self._mutex.lock() r = self._result self._result = None self._mutex.unlock() return r
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 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 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 RenderThread(QThread): """ Thread object that takes control over an actual object exposing thoses methods with immediate return with delayed responses. """ def __init__(self, parent): QThread.__init__(self, parent) self.classObjectLock = QMutex(QMutex.Recursive) self.classParamDict = {} self.classInstanceDict = {} self.queueLock = QMutex(QMutex.Recursive) self.queueHasJobsLock = QMutex() # lock lighter than queueLock # while waiting for this lock no # lock on the latter can be hold self.queueHasJobsCondition = QWaitCondition() self.renderQueue = [] # contains all render requests self.newestId = 0 # newest job Id self.renderingLock = QMutex(QMutex.Recursive) self.renderingFinishedLock = QMutex() # lock lighter than renderingLock # while waiting for this lock no # lock on the latter can be hold self.renderingFinishedCondition = QWaitCondition() self.currentlyRenderingJob = None def quit(self): self.dequeueAll() while self.classInstanceDict: classObject = self.classInstanceDict.keys()[0] self.removeObject(classObject) # enqueue meta command self.enqueue(None, 'quit') self.wait() def setObject(self, classObject, *args, **param): """Add or reset a class handled by this render thread.""" if not self.isRunning(): raise Exception( "Thread needs to be running before objects can be created.") self.classObjectLock.lock() if classObject in self.classParamDict: # TODO remove, or change reloadObject() #if self.classParamDict[classObject] == (args, param): #self.classObjectLock.unlock() ## no changes done #return self.removeObject(classObject) self.classParamDict[classObject] = (args, param) self.classInstanceDict[classObject] = None self.enqueueWait(classObject, '__init__', *args, **param) #self.enqueue(classObject, '__init__', *args, **param) ## return only once object is created #self.renderingFinishedLock.lock() #while not self.classInstanceDict[classObject]: #self.renderingFinishedCondition.wait(self.renderingFinishedLock) #self.renderingFinishedLock.unlock() #self.classObjectLock.unlock() def reloadObject(self, classObject): """Reloads a class object.""" self.classObjectLock.lock() if classObject not in self.classParamDict: self.classObjectLock.unlock() raise Exception("Object not set") args, param = self.classParamDict[classObject] self.setObject(classObject, *args, **param) self.classObjectLock.unlock() def hasObject(self, classObject): """returns the object's instance created by the thread.""" QMutexLocker(self.classObjectLock) return classObject in self.classInstanceDict def getObjectInstance(self, classObject): """returns the object's instance created by the thread.""" QMutexLocker(self.classObjectLock) return self.classInstanceDict[classObject] def removeObject(self, classObject): """ Removes the given object's instance from the render thread, removing all affiliated jobs from the queue and canceling an eventually current rendered method. """ self.classObjectLock.lock() self.queueHasJobsLock.lock() self.queueLock.lock() # clear all not yet rendered content from the queue newQueue = [] for entry in self.renderQueue: jobId, entryClassObject, _, _, _ = entry if classObject != entryClassObject: newQueue.append(entry) else: self.emit(SIGNAL("jobDequeued"), jobId) self.renderQueue = newQueue # interrupt currently rendering self.renderingLock.lock() if self.currentlyRenderingJob: _, entryClassObject, _, _, _ = self.currentlyRenderingJob if classObject == entryClassObject: self.cancelCurrentJob() self.renderingLock.unlock() del self.classParamDict[classObject] del self.classInstanceDict[classObject] self.queueHasJobsLock.unlock() self.queueLock.unlock() self.classObjectLock.unlock() def enqueue(self, classObject, method, *args, **param): self.classObjectLock.lock() if classObject and classObject not in self.classParamDict: self.classObjectLock.unlock() raise Exception("Object not set") self.queueHasJobsLock.lock() self.queueLock.lock() self.newestId = (self.newestId + 1) % sys.maxint jobId = self.newestId self.renderQueue.append((jobId, classObject, method, args, param)) self.queueLock.unlock() self.queueHasJobsCondition.wakeAll() self.queueHasJobsLock.unlock() self.classObjectLock.unlock() if classObject: self.emit(SIGNAL("jobEnqueued"), jobId) return jobId def enqueueWait(self, classObject, method, *args, **param): """Enqueues a job and waits until it finishes.""" def inQueue(jobId): QMutexLocker(self.queueLock) for qjobId, _, _, _, _ in self.renderQueue: if jobId == qjobId: return True QMutexLocker(self.renderingLock) if self.currentlyRenderingJob: qjobId, _, _, _, _ = self.currentlyRenderingJob if jobId == qjobId: return True return False jobId = self.enqueue(classObject, method, *args, **param) if jobId == None: return # return only once method has finished self.renderingFinishedLock.lock() while inQueue(jobId): self.renderingFinishedCondition.wait(self.renderingFinishedLock) self.renderingFinishedLock.unlock() return jobId def getJobEntry(self, jobId): self.queueLock.lock() for idx, entry in enumerate(self.renderQueue): entryJobId, _, _, _, _ = entry if entryJobId == jobId: break else: entryJobId = None self.queueLock.unlock() return entryJobId def dequeue(self, jobId): QMutexLocker(self.queueHasJobsLock) QMutexLocker(self.queueLock) # search for entry jobEntry = self.getJobEntry(jobId) if jobEntry: idx = self.renderQueue.index(jobEntry) del self.renderQueue[idx] self.emit(SIGNAL("jobDequeued"), jobId) return True else: # interrupt currently rendering QMutexLocker(self.renderingLock) if self.currentlyRenderingJob: entryJobId, _, _, _, _ = self.currentlyRenderingJob if entryJobId == jobId: self.cancelCurrentJob() return True return False def dequeueMethod(self, classObject, method): self.queueHasJobsLock.lock() self.queueLock.lock() # clear all not yet rendered content from the queue newQueue = [] for entry in self.renderQueue: jobId, entryClassObject, entryMethod, _, _ = entry if classObject != entryClassObject or method != entryMethod: newQueue.append(entry) else: self.emit(SIGNAL("jobDequeued"), jobId) self.renderQueue = newQueue # interrupt currently rendering self.renderingLock.lock() if self.currentlyRenderingJob: _, entryClassObject, entryMethod, _, _ = self.currentlyRenderingJob if classObject == entryClassObject and method == entryMethod: self.cancelCurrentJob() self.renderingLock.unlock() self.queueLock.unlock() self.queueHasJobsLock.unlock() def dequeueAll(self): self.queueHasJobsLock.lock() self.queueLock.lock() # signal all for jobId, _, _, _, _ in self.renderQueue: self.emit(SIGNAL("jobDequeued"), jobId) self.renderQueue = [] # interrupt currently rendering self.renderingLock.lock() if self.currentlyRenderingJob: self.cancelCurrentJob() self.renderingLock.unlock() self.queueLock.unlock() self.queueHasJobsLock.unlock() def isRendering(self): self.queueLock.lock() self.renderingLock.lock() rendering = len(self.renderQueue) > 0 \ or self.currentlyRenderingJob != None self.renderingLock.unlock() self.queueLock.unlock() return rendering def cancelCurrentJob(self): """ This method is called when the currently rendered job should be canceled. The default implementation doesn't handle any object specific cancel operations and by default returns False. An actual implementation should call the current object's cancel routine and then call clearCurrentJob() to clear the current job and finally return True. """ return False def clearCurrentJob(self): """ This method needs to be called before the current job is canceled. """ self.renderingLock.lock() self.currentlyRenderingJob = None self.renderingLock.unlock() def run(self): while True: self.queueHasJobsLock.lock() hasJobWaiting = len(self.renderQueue) > 0 while not hasJobWaiting: self.emit(SIGNAL("queueEmpty")) self.queueHasJobsCondition.wait(self.queueHasJobsLock) hasJobWaiting = len(self.renderQueue) > 0 self.queueLock.lock() self.renderingLock.lock() self.currentlyRenderingJob = self.renderQueue.pop(0) jobId, entryClassObject, method, args, param \ = self.currentlyRenderingJob self.renderingLock.unlock() self.queueLock.unlock() self.queueHasJobsLock.unlock() if entryClassObject != None: if method == '__init__': classInstance = entryClassObject(*args, **param) self.classInstanceDict[entryClassObject] = classInstance self.emit(SIGNAL("objectCreated"), jobId, entryClassObject) else: classInstance = self.classInstanceDict[entryClassObject] try: content = getattr(classInstance, method)(*args, **param) self.finishJob(jobId, entryClassObject, method, args, param, content) except BaseException, e: if self.currentlyRenderingJob: stacktrace = traceback.format_exc() self.emit(SIGNAL("jobErrorneous"), jobId, entryClassObject, method, args, param, e, stacktrace) else: # job got canceled self.emit(SIGNAL("jobCanceled"), jobId, entryClassObject, method, args, param) self.renderingFinishedLock.lock() self.clearCurrentJob() self.renderingFinishedCondition.wakeAll() self.renderingFinishedLock.unlock() if entryClassObject == None and method == 'quit': return
class _FTSearchThread(QThread): '''This thread performs full text search in the background''' searchFinished = pyqtSignal() searchError = pyqtSignal() def __init__(self, searcher, parent): QThread.__init__(self, parent) self._searcher = searcher self._quit = False self._mutex = QMutex() self._pending = QWaitCondition() self._collector = None self._query = None self._result = None def run(self): while not self._quit: self._mutex.lock() if not self._query: self._pending.wait(self._mutex) query = self._query self._query = None self._mutex.unlock() # search if query: (query_str1, query_str2, itemtypes, limit, highlight, merge) = query self._mutex.lock() collector = self._searcher.make_collector(limit) self._collector = collector self._mutex.unlock() try: result = self._searcher.search(collector, query_str1, query_str2, itemtypes, highlight) except: self._mutex.lock() self._result = None self._mutex.unlock() self.searchError.emit() else: if collector.aborted: pass else: self._mutex.lock() self._result = (merge, result) self._mutex.unlock() self.searchFinished.emit() self._mutex.lock() self._collector = None self._mutex.unlock() def cancel(self): self._mutex.lock() self._query = None if self._collector: self._collector.abort() self._mutex.unlock() def quit(self): self._mutex.lock() if self._collector: self._collector.abort() self._query = None self._quit = True self._mutex.unlock() self._pending.wakeAll() def update_query(self, query_str1=None, query_str2=None, itemtypes=(), limit=1000, highlight=False, merge=False): self._mutex.lock() if self._collector: self._collector.abort() self._query = (query_str1, query_str2, itemtypes, limit, highlight, merge) self._mutex.unlock() self._pending.wakeAll() def take_result(self): self._mutex.lock() r = self._result self._result = None self._mutex.unlock() return r
def __init__(self, parent=None): QThread.__init__(self, parent) self.waitcond = QWaitCondition() self.mutex = QMutex() self.isstopped = False
class TestThread(QThread): def __init__(self, suite, items, stopOnError, stopOnFail, xmlRPCUrl): QThread.__init__(self) self.suite = suite self.items = items executionDelay = self.suite.sliderSpeed.value() / 1000 # convert to seconds self.sync = QMutex() self.pauseCond = QWaitCondition() self.executionPaused = False self.autoPlayer = AutoPlayer(executionDelay, self.sync, self.pauseCond, xmlRPCUrl) self.executionStopped = False self.stopOnError = stopOnError self.stopOnFail = stopOnFail self._connectSignals() def _connectSignals(self): self.connect(self.suite,SIGNAL("pauseExecution"), self.pauseExecution) self.connect(self.suite,SIGNAL("unpauseExecution"), self.unpauseExecution) self.connect(self.suite,SIGNAL("executionStopped"), self.stopExecution) self.connect(self.autoPlayer,SIGNAL("logMessage"), self.suite.logMessage) self.connect(self.suite.sliderSpeed, SIGNAL('valueChanged(int)'), self.updateExecutionSpeed) def checkForPause(self): """ Check if the pause flag has been set""" self.sync.lock() if(self.executionPaused): self.pauseCond.wait(self.sync) self.sync.unlock() def run(self): self.emit(SIGNAL('updateExecutionButtons'), False) # set default icon for all items for item in self.items: self.emit(SIGNAL('updateItemStatus'), item, TestCaseStatus.UNTESTED, False) # parse and execute test caeses handNumber = 1 failedTc = 0 successfulTc = 0 errorTc = 0 for item in self.items: try: self.checkForPause() if self.executionStopped: return self.emit(SIGNAL('logMessage'), " => Test case : " + item.text(),LogStyle.TITLE) self.emit(SIGNAL('updateItemStatus'), item, TestCaseStatus.UNTESTED, True) tc = item.data(Qt.UserRole) # get file name #parse test case tcp = TestCaseParser(tc.file) tcp.parse() #start test successful = self.autoPlayer.startTest(tcp, handNumber) self.checkForPause() if self.executionStopped: return if successful: self.emit(SIGNAL('updateItemStatus'), item, TestCaseStatus.SUCCESS, True) successfulTc += 1 else: self.emit(SIGNAL('updateItemStatus'), item, TestCaseStatus.FAILED, True) failedTc += 1 if(self.stopOnFail): break handNumber += 1 except ParserException as e: self.emit(SIGNAL('updateItemStatus'), item, TestCaseStatus.ERROR, True) self.emit(SIGNAL('logMessage'),"Parsing error: " + str(e),LogStyle.WARNING) errorTc += 1 if(self.stopOnError): break except socket.error: self.emit(SIGNAL('logMessage'),"Can't connect to player",LogStyle.ERROR) self.stopExecution() self.emit(SIGNAL('updateExecutionButtons'), True) return except Exception as e: self.emit(SIGNAL('logMessage'),"Unknown error: " + str(e),LogStyle.ERROR) raise self.emit(SIGNAL('logMessage'), " => Execution finished",LogStyle.TITLE) if(len(self.items) > 1): self.emit(SIGNAL('logMessage'), "Result => Total: {0}, Successful: {1}, Failed: {2}, Error: {3}" .format(len(self.items),successfulTc,failedTc,errorTc),LogStyle.TITLE) self.emit(SIGNAL('updateExecutionButtons'), True) def pauseExecution(self): """ Pause test case execution """ self.sync.lock() self.executionPaused = True self.autoPlayer.pause() self.sync.unlock() def unpauseExecution(self): """ Unpause test case execution """ self.sync.lock() self.executionPaused = False self.autoPlayer.unpause() self.pauseCond.wakeAll() self.sync.unlock() def stopExecution(self): """ Stop test case execution """ if(self.executionPaused): self.unpauseExecution() self.executionStopped = True self.autoPlayer.stop() def updateExecutionSpeed(self, value): executionDelay = value / 1000 # convert to seconds self.autoPlayer.executionDelay = executionDelay
def __init__(self, parent = None): QThread.__init__(self, parent) self.waitcond = QWaitCondition() self.mutex = QMutex() self.isstopped = False
def __init__(self): QThread.__init__(self) self.cond = QWaitCondition()