Example #1
0
 def __init__(self, maximum, parent=None):
     QThread.__init__(self, parent)
     self.maximum = maximum
     # this semaphore is used to stop the thread from outside.  As long
     # as the thread is permitted to run, it has yet a single resource
     # available.  If the thread is stopped, this resource is acquired,
     # and the thread breaks its counting loop at the next iteration.
     self._run_semaphore = QSemaphore(1)
Example #2
0
class Counter(QThread):

    #: this signal is emitted whenever the worker has performed a single
    #  step
    progress = Signal(int)
    #: this signal publishes the minimum amount of required steps
    minimumChanged = Signal(int)
    #: this signal publishes the maximum amount of required steps
    maximumChanged = Signal(int)

    def __init__(self, maximum, parent=None):
        QThread.__init__(self, parent)
        self.maximum = maximum
        # this semaphore is used to stop the thread from outside.  As long
        # as the thread is permitted to run, it has yet a single resource
        # available.  If the thread is stopped, this resource is acquired,
        # and the thread breaks its counting loop at the next iteration.
        self._run_semaphore = QSemaphore(1)

    def run(self):
        # published our minimum and maximum values.  A real worker thread
        # would typically calculate the number of required steps
        self.minimumChanged.emit(1)
        self.maximumChanged.emit(self.maximum)
        # now do our work, which is simply counting in this example
        for i in range(1, self.maximum + 1):
            self.progress.emit(i)
            # check, if the thread is still permitted to run.  This test
            # fails, if the single resource of this semaphore is acquired,
            # which is done by the stop() method, just to stop work at
            # precisly this point
            if self._run_semaphore.available() == 0:
                # release the resource again to enable a restart of this worker
                self._run_semaphore.release(1)
                # break the loop.  A real worker would typically roll back
                # his work to leave the state of the application unchanged,
                # because the work was forcibly interrupted.
                break
            # simulate some heavy work
            self.msleep(100)

    def stop(self):
        # acquire the single resource of our run semaphore.  No resources
        # are now available anymore, so next time, run() checks the number
        # of available resources, it sees none and returns.
        self._run_semaphore.acquire(1)
 def __init__(self, maximum, parent=None):
     QThread.__init__(self, parent)
     self.maximum = maximum
     # this semaphore is used to stop the thread from outside.  As long
     # as the thread is permitted to run, it has yet a single resource
     # available.  If the thread is stopped, this resource is acquired,
     # and the thread breaks its counting loop at the next iteration.
     self._run_semaphore = QSemaphore(1)
class Counter(QThread):

    #: this signal is emitted whenever the worker has performed a single
    #  step
    progress = Signal(int)
    #: this signal publishes the minimum amount of required steps
    minimumChanged = Signal(int)
    #: this signal publishes the maximum amount of required steps
    maximumChanged = Signal(int)

    def __init__(self, maximum, parent=None):
        QThread.__init__(self, parent)
        self.maximum = maximum
        # this semaphore is used to stop the thread from outside.  As long
        # as the thread is permitted to run, it has yet a single resource
        # available.  If the thread is stopped, this resource is acquired,
        # and the thread breaks its counting loop at the next iteration.
        self._run_semaphore = QSemaphore(1)

    def run(self):
        # published our minimum and maximum values.  A real worker thread
        # would typically calculate the number of required steps
        self.minimumChanged.emit(1)
        self.maximumChanged.emit(self.maximum)
        # now do our work, which is simply counting in this example
        for i in range(1, self.maximum+1):
            self.progress.emit(i)
            # check, if the thread is still permitted to run.  This test
            # fails, if the single resource of this semaphore is acquired,
            # which is done by the stop() method, just to stop work at
            # precisly this point
            if self._run_semaphore.available() == 0:
                # release the resource again to enable a restart of this worker
                self._run_semaphore.release(1)
                # break the loop.  A real worker would typically roll back
                # his work to leave the state of the application unchanged,
                # because the work was forcibly interrupted.
                break
            # simulate some heavy work
            self.msleep(100)

    def stop(self):
        # acquire the single resource of our run semaphore.  No resources
        # are now available anymore, so next time, run() checks the number
        # of available resources, it sees none and returns.
        self._run_semaphore.acquire(1)
 def __init__(self):
     super(AsyncResolver, self).__init__()
     self._task_queue = []
     self._mutex = QMutex()
     self._semaphore = QSemaphore()
     self.working = False
class AsyncResolver(QThread):

    object_resolved = QtCore.Signal('object_resolved(QVariant)')

    def __init__(self):
        super(AsyncResolver, self).__init__()
        self._task_queue = []
        self._mutex = QMutex()
        self._semaphore = QSemaphore()
        self.working = False

    def synchronized(f):
        def wrap(self, *args, **kwargs):
            self._mutex.lock()
            try:
                return f(self, *args, **kwargs)
            finally:
                self._mutex.unlock()
        return wrap

    def add_task(self, dn):
        self._task_queue.append(dn)
        #print self._semaphore.available()
        self._semaphore.release()
        #print 'before release'

    @synchronized
    def _append_task(self, task):
        self._task_queue.append(task)

    @synchronized
    def _pop_task(self, task):
        return self._task_queue.pop()

    def consume_task(self):
        #print self._semaphore.available()
        self._semaphore.acquire()
        #print 'after acq'
        obj = self._task_queue.pop()
        return obj

    def stop_work(self):
        self.working = False
        if self._task_queue:
            self._append_task(None)
        self._semaphore.release()

    @synchronized
    def _do_task(self, task):
        return task()

    def run(self):
        self.working = True
        while self.working:
            task = self.consume_task()
            if task:
                try:
                    obj = self._do_task(task)
                except UcsmError:
                    pass
                else:
                    self.emit(SIGNAL('object_resolved(QVariant)'), obj)
            else:
                break