def process_states(self, states, use_optimizations, output_mode, plot_results, output_graph): self._worker = Worker(self._process_states_on_thread, states, use_optimizations, output_mode, plot_results, output_graph) self._worker.signals.finished.connect(self.on_finished) self._worker.signals.error.connect(self.on_error) QThreadPool.globalInstance().start(self._worker)
def on_send_status_clicked(self): status = self.ui.status.toPlainText().__str__() self.ui.status.clear() self.ui.status.setReadOnly(True) sender = SenderCommand(self.client, status) sender.posted.connect(self.status_posted) command_processor = CommandProcessor(sender) QThreadPool.globalInstance().start(command_processor)
def run_worker(self, task, callback): """Runs a task in another thread. The `task` must be an object that implements a `run()` method. Completion is notified to the given `callback` function. """ worker = Worker(task) worker.finished.connect(callback) QThreadPool.globalInstance().start(worker)
def process(self, caller, func, *args, **kwargs): # Add the caller self._add_listener(caller) # Generate worker self._worker = Worker(func, *args, **kwargs) self._worker.signals.finished.connect(self.on_finished) self._worker.signals.error.connect(self.on_error) QThreadPool.globalInstance().start(self._worker)
def process(self, caller, func, *args, **kwargs): # Add the caller self._add_listener(caller) # Generate worker self._worker = Worker(func, *args, **kwargs) self._worker.signals.finished.connect(self.on_finished) self._worker.signals.error.connect(self.on_error) QThreadPool.globalInstance().start(self._worker)
def run_worker(self, task, callback): """Runs a task in another thread. The `task` must be an object that implements a `run()` method. Completion is notified to the given `callback` function. """ worker = Worker(task) worker.finished.connect(callback) QThreadPool.globalInstance().start(worker)
def query(self, key): """Emit query.""" collect = Collect( list(map(lambda a: a.engine, filter(Group.selected, self.output_areas))), lambda r: self.record(key, r) ) QThreadPool.globalInstance().start(collect) #for engine in self.engines: #QThreadPool.globalInstance().start( #Runnable(lambda: engine.query(key), args=(object,))) async_actions(map(lambda e: partial(e.query, key), self.engines))
def test_that_process_states_emits_row_processed_signal_after_each_row(self): self.batch_process_runner.row_processed_signal = mock.MagicMock() self.batch_process_runner.row_failed_signal = mock.MagicMock() self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '') QThreadPool.globalInstance().waitForDone() self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 3) self.batch_process_runner.row_processed_signal.emit.assert_any_call(0, [], []) self.batch_process_runner.row_processed_signal.emit.assert_any_call(1, [], []) self.batch_process_runner.row_processed_signal.emit.assert_any_call(2, [], []) self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 0)
def test_that_process_states_emits_row_processed_signal_after_each_row(self): self.batch_process_runner.row_processed_signal = mock.MagicMock() self.batch_process_runner.row_failed_signal = mock.MagicMock() self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '') QThreadPool.globalInstance().waitForDone() self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 3) self.batch_process_runner.row_processed_signal.emit.assert_any_call(0) self.batch_process_runner.row_processed_signal.emit.assert_any_call(1) self.batch_process_runner.row_processed_signal.emit.assert_any_call(2) self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 0)
def test_that_load_workspaces_emits_row_processed_signal_after_each_row(self): self.batch_process_runner.row_processed_signal = mock.MagicMock() self.batch_process_runner.row_failed_signal = mock.MagicMock() self.batch_process_runner.load_workspaces(self.states) QThreadPool.globalInstance().waitForDone() self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 3) self.batch_process_runner.row_processed_signal.emit.assert_any_call(0, [], []) self.batch_process_runner.row_processed_signal.emit.assert_any_call(1, [], []) self.batch_process_runner.row_processed_signal.emit.assert_any_call(2, [], []) self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 0)
def test_that_process_states_emits_row_failed_signal_after_each_failed_row(self): self.batch_process_runner.row_processed_signal = mock.MagicMock() self.batch_process_runner.row_failed_signal = mock.MagicMock() self.sans_batch_instance.side_effect = Exception('failure') self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '') QThreadPool.globalInstance().waitForDone() self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 3) self.batch_process_runner.row_failed_signal.emit.assert_any_call(0, 'failure') self.batch_process_runner.row_failed_signal.emit.assert_any_call(1, 'failure') self.batch_process_runner.row_failed_signal.emit.assert_any_call(2, 'failure') self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 0)
def record(self, key, responses): """Record in background.""" if not self.recorder is None: runnable = Runnable(lambda: self.recorder.add( #word=s.word, #pron=s.phonetic_symbol, #trans='\n'.join(s.custom_translations), #time=datetime.now() key=key, responses=responses )) runnable.finished.connect(self.recorded) QThreadPool.globalInstance().start(runnable)
def push_to_website(payload): time = payload[SENSOR_TIMESTAMP]["val"] for key in payload: category = payload[key]["cat"] value = payload[key]["val"] if category == CAT_NONE: continue api = WebsiteAPI() api.set_password(WEB_PASSWORD) api.set_host(WEB_HOST) api.set_data(category, key, value, time) QThreadPool.globalInstance().start(api)
def __init__(self, func=None, args=(), kwargs={}, thread=None, threadPool=None, parent=None): QObject.__init__(self, parent) self.func = func self._args = args self._kwargs = kwargs self.threadPool = None self._connected = True self._cancelRequested = False self._started = False self._cancelled = False if thread is not None: self.moveToThread(thread) else: if threadPool is None: threadPool = QThreadPool.globalInstance() self.threadPool = threadPool self._runnable = _RunnableAsyncCall(self) self.threadPool.start(self._runnable) self._connected = False return self.connect(self, SIGNAL("_async_start()"), self.execute, Qt.QueuedConnection)
def __init__(self, func=None, args=(), kwargs={}, thread=None, threadPool=None, parent=None): QObject.__init__(self, parent) self.func = func self._args = args self._kwargs = kwargs self.threadPool = None self._connected = True self._cancelRequested = False self._started = False self._cancelled = False if thread is not None: self.moveToThread(thread) else: if threadPool is None: threadPool = QThreadPool.globalInstance() self.threadPool = threadPool self._runnable = _RunnableAsyncCall(self) self.threadPool.start(self._runnable) self._connected = False return self.connect(self, SIGNAL("_async_start()"), self.execute, Qt.QueuedConnection)
def test_that_load_workspaces_emits_row_processed_signal_after_each_row( self): self.batch_process_runner.row_processed_signal = mock.MagicMock() self.batch_process_runner.row_failed_signal = mock.MagicMock() self.batch_process_runner.load_workspaces(self.states) QThreadPool.globalInstance().waitForDone() self.assertEqual( self.batch_process_runner.row_processed_signal.emit.call_count, 3) self.batch_process_runner.row_processed_signal.emit.assert_any_call( 0, [], []) self.batch_process_runner.row_processed_signal.emit.assert_any_call( 1, [], []) self.batch_process_runner.row_processed_signal.emit.assert_any_call( 2, [], []) self.assertEqual( self.batch_process_runner.row_failed_signal.emit.call_count, 0)
def __init__(self, parent=None, threadPool=None): QObject.__init__(self, parent) if threadPool is None: threadPool = QThreadPool.globalInstance() self._threadPool = threadPool self._depot_thread = None self._futures = [] self._shutdown = False self._state_lock = threading.Lock()
def login(self): assert self.settings.username,\ 'Invalid username: %s' % self.settings.username def inner(): try: self.recorder = ThreadSafeRecorder(Recorder( username=self.settings.username, password=self.settings.password, deck=self.settings.deck )) except: return False else: return True runnable = Runnable(inner) runnable.finished.connect(self.logined) QThreadPool.globalInstance().start(runnable)
def __init__(self, # A number *n* to create a pool of *n* simple threads, where each thread # lacks an event loop, so that it can emit but not receive signals. # This means that ``g`` may **not** be run in a thread of this pool, # without manually adding a event loop. If *n* < 1, the global thread pool # is used. maxThreadCount, # |parent| parent=None): super(AsyncPoolController, self).__init__(parent) if maxThreadCount < 1: self.threadPool = QThreadPool.globalInstance() else: self.threadPool = QThreadPool() self.threadPool.setMaxThreadCount(maxThreadCount)
def setUp(self): "" self.app = QApplication(sys.argv) mon = QThreadPool.globalInstance()
def test_that_process_states_calls_batch_reduce_for_each_row(self): self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '') QThreadPool.globalInstance().waitForDone() self.assertEqual(self.sans_batch_instance.call_count, 3)
def query(self, key): self.query_start.emit() runnable = Runnable(lambda: self.impl.query(key), args=(object,)) runnable.finished.connect(self.query_finished) QThreadPool.globalInstance().start(runnable)
# q.run() # logger.log(" : ".join[mname,tid,"result",res]) ### gui based ### def open_file_browser(): "" path = QtGui.QFileDialog.getOpenFileName(None,QString.fromLocal8Bit("Select config file:"),"*.ini") # if path: # self.database = path # To make possible cancel the FileDialog and continue loading a predefined db # self.openDBFile() return path # @log # def open_window(): # "" if __name__ == '__main__': app = QtGui.QApplication(sys.argv) mon = QThreadPool.globalInstance() print mon.activeThreadCount() sys.exit(app.exec_())
def tearDownClass(cls): TestQgsPalLabeling.tearDownClass() # avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760 QThreadPool.globalInstance().waitForDone()
def __init__(self): super(Tasks, self).__init__() self.pool = QThreadPool.globalInstance() self.pool.setMaxThreadCount(2)
def start(self): QThreadPool.globalInstance().start(self.worker)
def __init__(self): super(Tasks, self).__init__() self.pool = QThreadPool.globalInstance() self.pool.setMaxThreadCount(2)
def __init__(self, ): # Consider using multiple QStringListModels # QVXYModelMapper can map columns or rows to an xy-model super(Main, self).__init__() dpiX = self.logicalDpiX() self.setupUi(self) self.action_Exit.triggered.connect(self.close) self.modelShape = TransducerShapeModel(["Linear", "Linear (focused)", "Curvelinear", "Curvelinear (focused)"]) # TODO: connect to model, which emits updated #self.cboxElePlacement.view().selectionModel().selectionChanged.connect(self.modelElevationPlacement.update) # TODO: update parameter, which is used for selecting inner or output #self.modelElevationPlacement.updated.connect(self.proxy.on_elevation_placement_select) self.cboxShape.setModel(self.modelShape) self.cboxShape.view().selectionModel().selectionChanged.connect(self.modelShape.test) # model emits select # TODO: How to use a model for a comboBox??? #self.modelElevationPlacement = ElevationPlacementModel(["Inner", "Outer"]) #self.cboxElePlacement.setModel(self.modelElevationPlacement) #self.cboxElePlacement.view().selectionModel().selectionChanged.connect(self.update_image) # Temporary solution without model self.cboxElePlacement.setCurrentIndex(0) self.cboxElePlacement.currentIndexChanged[QString].connect(self.on_cbox_ele_placement_changed) self.delegate = TransducerElementDelegate(self) # Baffle is disabled for CW (default) self.cboxBaffle.setDisabled(1) # CW defaults defaults = [128, 3e-3, 5.0e-4, 3.5e-3, 61e-3, 5, 50e-3, 8.5e-2] # PW defaults defaults = [64, 1.8e-4, 2e-5, 2e-4, 61e-3, 5, 20e-3, 1.5e-2] self.model = TransducerElementModel(defaults, self) self.proxy = TransducerElementModelProxy(self) self.proxy.setSourceModel(self.model) self.modelShape.select.connect(self.proxy.on_transducer_shape_select) # select triggers update self.tblElements.setModel(self.proxy) self.tblElements.setItemDelegate(self.delegate) self.tblElements.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch) #QtGui.QHeaderView.ResizeToContents self.tblElements.verticalHeader().setResizeMode(QtGui.QHeaderView.Fixed) self.tblElements.setVisible(False) self.tblElements.resizeColumnsToContents() self.tblElements.resizeRowsToContents() self.tblElements.setVisible(True) self.pool = QThreadPool.globalInstance() self.pool.setMaxThreadCount(4) self.active = False self.proxy.dataChanged.connect(self.update_image) #self.modelElevationPlacement.dataChanged.connect(self.update_image) #self.modelElevationPlacement.updated.connect(self.update_image) self.cboxShape.view().selectionModel().selectionChanged.connect(self.update_image) # TODO: Make model work #self.propModel = PropagatorModel(["Point", "Plane", "Sphere"], self) #self.propModel = PropagatorModel(["Sphere"], self) #self.cboxPropagator.setModel(self.propModel) self.sboxOrder.setValue(40) self.sboxOrder.setMinimum(2) self.sboxOrder.setMaximum(500) self.cboxDomain.setCurrentIndex(0) self.cboxDomain.currentIndexChanged[QString].connect(self.on_cbox_domain_changed) self.cboxDomain.setDisabled(True) self.leSampleFrq.setDisabled(True) self.leExcitationCycles.setDisabled(True) self.sboxOrder.valueChanged[int].connect(self.on_sbox_order_changed) # CW grid data = [[170, 1, 250], [0, 0, 125], [ 0.004, 0.1, 0.0036], [-0.338, 0.00, 0.0018], [ 0.3380, 0.00, 0.8982]] # PW grid data = [[100, 1, 100], [0, 0, 60], [ 6e-5, 0.10, 4e-4], [-0.003, 0.00, 0.00], [ 0.003, 0.00, 0.04]] self.gridXYZ = GridTableModel(data, ['X','Y','Z'], ['N','offset', 'delta','min','max'], self) self.tblGrid.setModel(self.gridXYZ) self.tblGrid.setItemDelegate(self.delegate) self.tblGrid.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch) self.tblGrid.verticalHeader().setResizeMode(QtGui.QHeaderView.Fixed) self.tblGrid.setVisible(False) self.tblGrid.resizeColumnsToContents() self.tblGrid.resizeRowsToContents() self.gridDelegate = GridElementDelegate(self) self.readOnlyDelegate = ReadOnlyDelegate(self) self.tblGrid.setItemDelegateForRow(0,self.gridDelegate) self.tblGrid.setItemDelegateForRow(1,self.gridDelegate) self.tblGrid.setItemDelegateForRow(2,self.gridDelegate) self.tblGrid.setItemDelegateForRow(3,self.readOnlyDelegate) self.tblGrid.setItemDelegateForRow(4,self.readOnlyDelegate) self.tblGrid.setVisible(True) self.progressBar.setRange(0,100) self.leFNumber.setAlignment(Qt.AlignRight) self.btnField.clicked.connect(self.on_calc_clicked)
def __init__(self, parent=None, threadPool=None): QObject.__init__(self, parent) if threadPool is None: threadPool = QThreadPool.globalInstance() self._threadPool = threadPool self._depot_thread = None
def async_actions(actions): start = QThreadPool.globalInstance().start list(map(lambda a: start(Runnable(a, args=())), actions))
def deffer_to_thread(self, cb, *args, **kwargs): args = (self, ) + args thread = Thread(self, cb, *args, **kwargs) QThreadPool.globalInstance().start(thread)
def __init__(self, parent=None, threadPool=None): QObject.__init__(self, parent) if threadPool is None: threadPool = QThreadPool.globalInstance() self._threadPool = threadPool self._depot_thread = None
def deffer_to_thread(self, cb, *args, **kwargs): args = (self, ) + args thread = Thread(self, cb, *args, **kwargs) QThreadPool.globalInstance().start(thread)
def __init__(self, ): # Consider using multiple QStringListModels # QVXYModelMapper can map columns or rows to an xy-model super(Main, self).__init__() dpiX = self.logicalDpiX() self.setupUi(self) self.action_Exit.triggered.connect(self.close) self.modelShape = TransducerShapeModel([ "Linear", "Linear (focused)", "Curvelinear", "Curvelinear (focused)" ]) # TODO: connect to model, which emits updated #self.cboxElePlacement.view().selectionModel().selectionChanged.connect(self.modelElevationPlacement.update) # TODO: update parameter, which is used for selecting inner or output #self.modelElevationPlacement.updated.connect(self.proxy.on_elevation_placement_select) self.cboxShape.setModel(self.modelShape) self.cboxShape.view().selectionModel().selectionChanged.connect( self.modelShape.test) # model emits select # TODO: How to use a model for a comboBox??? #self.modelElevationPlacement = ElevationPlacementModel(["Inner", "Outer"]) #self.cboxElePlacement.setModel(self.modelElevationPlacement) #self.cboxElePlacement.view().selectionModel().selectionChanged.connect(self.update_image) # Temporary solution without model self.cboxElePlacement.setCurrentIndex(0) self.cboxElePlacement.currentIndexChanged[QString].connect( self.on_cbox_ele_placement_changed) self.delegate = TransducerElementDelegate(self) # Baffle is disabled for CW (default) self.cboxBaffle.setDisabled(1) # CW defaults defaults = [128, 3e-3, 5.0e-4, 3.5e-3, 61e-3, 5, 50e-3, 8.5e-2] # PW defaults defaults = [64, 1.8e-4, 2e-5, 2e-4, 61e-3, 5, 20e-3, 1.5e-2] self.model = TransducerElementModel(defaults, self) self.proxy = TransducerElementModelProxy(self) self.proxy.setSourceModel(self.model) self.modelShape.select.connect( self.proxy.on_transducer_shape_select) # select triggers update self.tblElements.setModel(self.proxy) self.tblElements.setItemDelegate(self.delegate) self.tblElements.horizontalHeader().setResizeMode( QtGui.QHeaderView.Stretch) #QtGui.QHeaderView.ResizeToContents self.tblElements.verticalHeader().setResizeMode( QtGui.QHeaderView.Fixed) self.tblElements.setVisible(False) self.tblElements.resizeColumnsToContents() self.tblElements.resizeRowsToContents() self.tblElements.setVisible(True) self.pool = QThreadPool.globalInstance() self.pool.setMaxThreadCount(4) self.active = False self.proxy.dataChanged.connect(self.update_image) #self.modelElevationPlacement.dataChanged.connect(self.update_image) #self.modelElevationPlacement.updated.connect(self.update_image) self.cboxShape.view().selectionModel().selectionChanged.connect( self.update_image) # TODO: Make model work #self.propModel = PropagatorModel(["Point", "Plane", "Sphere"], self) #self.propModel = PropagatorModel(["Sphere"], self) #self.cboxPropagator.setModel(self.propModel) self.sboxOrder.setValue(40) self.sboxOrder.setMinimum(2) self.sboxOrder.setMaximum(500) self.cboxDomain.setCurrentIndex(0) self.cboxDomain.currentIndexChanged[QString].connect( self.on_cbox_domain_changed) self.cboxDomain.setDisabled(True) self.leSampleFrq.setDisabled(True) self.leExcitationCycles.setDisabled(True) self.sboxOrder.valueChanged[int].connect(self.on_sbox_order_changed) # CW grid data = [[170, 1, 250], [0, 0, 125], [0.004, 0.1, 0.0036], [-0.338, 0.00, 0.0018], [0.3380, 0.00, 0.8982]] # PW grid data = [[100, 1, 100], [0, 0, 60], [6e-5, 0.10, 4e-4], [-0.003, 0.00, 0.00], [0.003, 0.00, 0.04]] self.gridXYZ = GridTableModel(data, ['X', 'Y', 'Z'], ['N', 'offset', 'delta', 'min', 'max'], self) self.tblGrid.setModel(self.gridXYZ) self.tblGrid.setItemDelegate(self.delegate) self.tblGrid.horizontalHeader().setResizeMode( QtGui.QHeaderView.Stretch) self.tblGrid.verticalHeader().setResizeMode(QtGui.QHeaderView.Fixed) self.tblGrid.setVisible(False) self.tblGrid.resizeColumnsToContents() self.tblGrid.resizeRowsToContents() self.gridDelegate = GridElementDelegate(self) self.readOnlyDelegate = ReadOnlyDelegate(self) self.tblGrid.setItemDelegateForRow(0, self.gridDelegate) self.tblGrid.setItemDelegateForRow(1, self.gridDelegate) self.tblGrid.setItemDelegateForRow(2, self.gridDelegate) self.tblGrid.setItemDelegateForRow(3, self.readOnlyDelegate) self.tblGrid.setItemDelegateForRow(4, self.readOnlyDelegate) self.tblGrid.setVisible(True) self.progressBar.setRange(0, 100) self.leFNumber.setAlignment(Qt.AlignRight) self.btnField.clicked.connect(self.on_calc_clicked)
def test_that_process_states_calls_batch_reduce_for_each_row(self): self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '') QThreadPool.globalInstance().waitForDone() self.assertEqual(self.sans_batch_instance.call_count, 3)
def tearDownClass(cls): TestQgsPalLabeling.tearDownClass() cls.removeMapLayer(cls.layer) cls.layer = None # avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760 QThreadPool.globalInstance().waitForDone()
def load_workspaces(self, states): self._worker = Worker(self._load_workspaces_on_thread, states) self._worker.signals.finished.connect(self.on_finished) self._worker.signals.error.connect(self.on_error) QThreadPool.globalInstance().start(self._worker)