def _wait_for_done(self, timeout): """ Will not return until either timeout expires or future becomes "done". There is one potential deadlock situation here: The deadlock occurs if we await_result while at the same time, this future needs to await_result from another future ---> To be safe, don't use await_result() in a Qt slot... """ if self.cancelled(): raise CancelledError("Future was cancelled") # pragma: no-cover if not self.done(): self.timer_timeout = None if (timeout is not None) and timeout > 0: self._timer_timeout = MainThreadTimer(timeout * 1000) self._timer_timeout.timeout.connect(self._exit_loop) self._timer_timeout.start() self.loop = QtCore.QEventLoop() self.add_done_callback(self._exit_loop) self.loop.exec_() if self._timer_timeout is not None: if not self._timer_timeout.isActive(): return TimeoutError("Timeout occured") # pragma: no-cover else: self._timer_timeout.stop()
def sleep(delay): """ Sleeps for :code:`delay` seconds + runs the event loop in the background. * This function will never return until the specified delay in seconds is elapsed. * During the execution of this function, the qt event loop (== asyncio event-loop in pyrpl) continues to process events from the gui, or from other coroutines. * Contrary to time.sleep() or async.sleep(), this function will try to achieve a precision much better than 1 millisecond (of course, occasionally, the real delay can be longer than requested), but on average, the precision is in the microsecond range. * Finally, care has been taken to use low level system-functions to reduce CPU-load when no events need to be processed. More details on the implementation can be found on the page: `<https://github.com/lneuhaus/pyrpl/wiki/Benchmark-asynchronous-sleep-functions>`_. """ tic = default_timer() end_time = tic + delay # 1. CPU-free sleep for delay - 1ms if delay > 1e-3: new_delay = delay - 1e-3 loop = QtCore.QEventLoop() timer = MainThreadTimer(new_delay * 1000) timer.timeout.connect(loop.quit) timer.start() try: loop.exec_() except KeyboardInterrupt as e: # pragma: no-cover # try to recover from KeyboardInterrupt by finishing the current task timer.setInterval(1) timer.start() loop.exec_() raise e # 2. For high-precision, manually process events 1-by-1 during the last ms while default_timer() < end_time: APP.processEvents()
def _plot(self, timeout=3000): print("_plot ..............") subwindow_counter = self.smartMT._subwindow_counter loop = QtCore.QEventLoop() self.smartMT._plot_option._current_plot.plotting_completed.connect( loop.quit) self.smartMT._plot_option._current_plot.plotting_error.connect( loop.quit) def handleTimeout(): # timed out, stop loop if loop.isRunning(): loop.quit() self.fail( "GUI plotting timed out, maybe consider increasing timeout to wait longer" ) _click_area(self.smartMT._plot_option.ui.pushButton_plot) if timeout is not None: QtCore.QTimer.singleShot(timeout, handleTimeout) loop.exec_() # wait for plotting print("self.smartMT._subwindow_counter = {}".format( self.smartMT._subwindow_counter)) print("subwindow_counter = {}".format(subwindow_counter)) print("------------------------------------------------------") self.assertTrue(self.smartMT._subwindow_counter == subwindow_counter + 1, "no image created") # test if the image is created
def mqtt_connection(request): broker_location = os.path.join( request.config.rootdir, "internals/paho.mqtt.testing/interoperability/startbroker.py", ) broker_process = QtCore.QProcess() python_path = sys.executable arguments = [broker_location] configuration = "localhost_testing.conf" broker_dir = QtCore.QFileInfo(broker_location).absoluteDir() if broker_dir.exists(configuration): arguments += [ "-c", QtCore.QDir.toNativeSeparators(broker_dir.absoluteFilePath(configuration)), ] broker_process.setWorkingDirectory(broker_dir.absolutePath()) broker_process.start(python_path, arguments) if not broker_process.waitForStarted(): raise Exception("Could not start MQTT test broker.") max_tries = 6 for try_counter in range(max_tries): socket = QtNetwork.QTcpSocket() socket.connectToHost("localhost", 1883) if socket.waitForConnected(3000): yield "localhost" break loop = QtCore.QEventLoop() QtCore.QTimer.singleShot(5000, loop.quit) loop.exec_() print("Could not launch MQTT test broker.")
def load_blocking(self, view, *args): loop = QtCore.QEventLoop() view.loadFinished.connect(loop.quit) def unravel(): view.loadFinished.disconnect(loop.quit) view.loadFinished.disconnect(unravel) view.loadFinished.connect(unravel) view.load(*args) loop.exec_()
def wait_on_signal(signal, timeout=250): """Block loop until signal emitted, or timeout (ms) elapses.""" loop = QtCore.QEventLoop() signal.connect(loop.quit) yield if timeout is not None: QtCore.QTimer.singleShot(timeout, loop.quit) loop.exec_() signal.disconnect(loop.quit)
def wait_signal(pSignal, pTimeout=None): """ Block loop until signal emitted, or timeout (ms) elapses. """ loop = QtCore.QEventLoop() pSignal.connect(loop.quit) yield if pTimeout is not None: QtCore.QTimer.singleShot(pTimeout, loop.quit) loop.exec_() pass
def test_qeventloop_exec_(qtbot): """Test QEventLoop.exec_""" assert QtCore.QEventLoop.exec_ is not None event_loop = QtCore.QEventLoop(None) QtCore.QTimer.singleShot(100, event_loop.quit) event_loop.exec_()
def run(self_): self.thread = QtCore.QThread.currentThread() self.loop = QtCore.QEventLoop() self.loop.exec() self.finished.emit() self.isFinished = True