worker_thread.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Dorian Scholz
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from python_qt_binding.QtCore import QThread
34 
35 
36 class WorkerThread(QThread):
37  """
38  Run code in a separate thread, get notified upon completion and kill the thread synchronously.
39 
40  Convenience class built around a QThread
41  """
42 
43  def __init__(self, run_callback, finished_callback=None):
44  super(WorkerThread, self).__init__()
45  self.run = run_callback
46  self._finished_callback = finished_callback
47 
48  def start(self):
49  # reconnect finished signal
50  self.finished.connect(self._finished_handler)
51  super(WorkerThread, self).start()
52 
53  def _finished_handler(self):
54  # disconnect finished signal to avoid being called twice
55  self.finished.disconnect(self._finished_handler)
56  if self._finished_callback is not None:
57  self._finished_callback()
58 
59  def kill(self):
60  if self.isRunning():
61  # clean up
62  self.finished.disconnect(self._finished_handler)
63  self.terminate()
64  self.wait()
qt_gui_py_common.worker_thread.WorkerThread
Definition: worker_thread.py:36
qt_gui_py_common.worker_thread.WorkerThread.__init__
def __init__(self, run_callback, finished_callback=None)
Definition: worker_thread.py:43
qt_gui_py_common.worker_thread.WorkerThread._finished_callback
_finished_callback
Definition: worker_thread.py:46
qt_gui_py_common.worker_thread.WorkerThread.start
def start(self)
Definition: worker_thread.py:48
qt_gui_py_common.worker_thread.WorkerThread.run
run
Definition: worker_thread.py:45
qt_gui_py_common.worker_thread.WorkerThread.kill
def kill(self)
Definition: worker_thread.py:59
qt_gui_py_common.worker_thread.WorkerThread._finished_handler
def _finished_handler(self)
Definition: worker_thread.py:53


qt_gui_py_common
Author(s): Dorian Scholz
autogenerated on Sat Jun 25 2022 02:15:08