Пример #1
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)
Пример #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)