def run(self): """Method implementing thread loop that updates the plot""" while self.running: time.sleep(1) # Run plot update asynchronously concurrent.submitToQtMainThread(self.plot1d.addCurve, numpy.arange(1000), numpy.random.random(1000), resetzoom=False)
def run(self): """Method implementing thread loop that updates the plot""" while self.running: time.sleep(1) # Run plot update asynchronously concurrent.submitToQtMainThread( self.plot1d.addCurve, numpy.arange(1000), numpy.random.random(1000), resetzoom=False)
def flush(self): """If there is a signal queued up, send it now.""" if self.args is None or self.blockSignal: return False args, self.args = self.args, None concurrent.submitToQtMainThread(self.timer.stop) self.lastFlushTime = time() # self.emit(self.signal, *self.args) concurrent.submitToQtMainThread(self.sigDelayed.emit, args) # self.sigDelayed.emit(args) return True
def run(self): """Method implementing thread loop that updates the plot""" while self.running: time.sleep(1) # Run plot update asynchronously concurrent.submitToQtMainThread(self.plot2d.addImage, numpy.random.random(10000).reshape( 100, 100), resetzoom=False, legend=random.choice( ('img1', 'img2')))
def run(self): """Method implementing thread loop that updates the scatter data It produces adds scatter points every 10 ms or so, up to 1 million. """ count = 0 # Number of data points currently rendered # Init arrays that accumulate scatter points x = numpy.array((), dtype=numpy.float32) y = numpy.array((), dtype=numpy.float32) z = numpy.array((), dtype=numpy.float32) value = numpy.array((), dtype=numpy.float32) while self.running: time.sleep(0.01) # Generate new data points inclination = numpy.random.random(1000).astype( numpy.float32) * numpy.pi azimuth = numpy.random.random(1000).astype( numpy.float32) * 2. * numpy.pi radius = numpy.random.normal(loc=10., scale=.5, size=1000) newX = radius * numpy.sin(inclination) * numpy.cos(azimuth) newY = radius * numpy.sin(inclination) * numpy.sin(azimuth) newZ = radius * numpy.cos(inclination) newValue = numpy.random.random(1000).astype(numpy.float32) # Accumulate data points x = numpy.append(x, newX) y = numpy.append(y, newY) z = numpy.append(z, newZ) value = numpy.append(value, newValue) # Only append data if the previous one has been added if self.future_result is None or self.future_result.done(): if count > MAX_NUMBER_OF_POINTS: # Restart a new scatter plot asyn self.future_result = concurrent.submitToQtMainThread( self.scatter3d.setData, x, y, z, value) count = len(x) else: # Append data asynchronously self.future_result = concurrent.submitToQtMainThread( self._appendScatterData, x, y, z, value) count += len(x) # Reset accumulators x = numpy.array((), dtype=numpy.float32) y = numpy.array((), dtype=numpy.float32) z = numpy.array((), dtype=numpy.float32) value = numpy.array((), dtype=numpy.float32)
def testFromMainThread(self): """Call submitToQtMainThread from the main thread""" value1, value2 = 0, 1 future = concurrent.submitToQtMainThread(self._task, value1, value2=value2) self.assertTrue(future.done()) self.assertEqual(future.result(1), (value1, value2)) self.assertIsNone(future.exception(1)) future = concurrent.submitToQtMainThread(self._taskWithException) self.assertTrue(future.done()) with self.assertRaises(RuntimeError): future.result(1) self.assertIsInstance(future.exception(1), RuntimeError)
def run(self): """Method implementing thread loop that updates the scatter data It produces adds scatter points every 10 ms or so, up to 1 million. """ count = 0 # Number of data points currently rendered # Init arrays that accumulate scatter points x = numpy.array((), dtype=numpy.float32) y = numpy.array((), dtype=numpy.float32) z = numpy.array((), dtype=numpy.float32) value = numpy.array((), dtype=numpy.float32) while self.running: time.sleep(0.01) # Generate new data points inclination = numpy.random.random(1000).astype(numpy.float32) * numpy.pi azimuth = numpy.random.random(1000).astype(numpy.float32) * 2. * numpy.pi radius = numpy.random.normal(loc=10., scale=.5, size=1000) newX = radius * numpy.sin(inclination) * numpy.cos(azimuth) newY = radius * numpy.sin(inclination) * numpy.sin(azimuth) newZ = radius * numpy.cos(inclination) newValue = numpy.random.random(1000).astype(numpy.float32) # Accumulate data points x = numpy.append(x, newX) y = numpy.append(y, newY) z = numpy.append(z, newZ) value = numpy.append(value, newValue) # Only append data if the previous one has been added if self.future_result is None or self.future_result.done(): if count > MAX_NUMBER_OF_POINTS: # Restart a new scatter plot asyn self.future_result = concurrent.submitToQtMainThread( self.scatter3d.setData, x, y, z, value) count = len(x) else: # Append data asynchronously self.future_result = concurrent.submitToQtMainThread( self._appendScatterData, x, y, z, value) count += len(x) # Reset accumulators x = numpy.array((), dtype=numpy.float32) y = numpy.array((), dtype=numpy.float32) z = numpy.array((), dtype=numpy.float32) value = numpy.array((), dtype=numpy.float32)
def run(self, pos={'x0': 0, 'y0': 0}): """Method implementing thread loop that updates the plot It produces an image every 10 ms or so, and either updates the plot or skip the image """ while self.running: time.sleep(0.01) # Create image # width of peak sigma_x = 0.15 sigma_y = 0.25 # x and y positions x = numpy.linspace(-1.5, 1.5, Nx) y = numpy.linspace(-1.0, 1.0, Ny) xv, yv = numpy.meshgrid(x, y) signal = numpy.exp(-((xv - pos['x0'])**2 / sigma_x**2 + (yv - pos['y0'])**2 / sigma_y**2)) # add noise signal += 0.3 * numpy.random.random(size=signal.shape) # random walk of center of peak ('drift') pos['x0'] += 0.05 * (numpy.random.random() - 0.5) pos['y0'] += 0.05 * (numpy.random.random() - 0.5) # If previous frame was not added to the plot yet, skip this one if self.future_result is None or self.future_result.done(): # plot the data asynchronously, and # keep a reference to the `future` object self.future_result = concurrent.submitToQtMainThread( self.plot2d.addImage, signal, resetzoom=False)
def run(self, pos={'x0': 0, 'y0': 0}): """Method implementing thread loop that updates the plot It produces an image every 10 ms or so, and either updates the plot or skip the image """ while self.running: time.sleep(0.01) # Create image # width of peak sigma_x = 0.15 sigma_y = 0.25 # x and y positions x = numpy.linspace(-1.5, 1.5, Nx) y = numpy.linspace(-1.0, 1.0, Ny) xv, yv = numpy.meshgrid(x, y) signal = numpy.exp(- ((xv - pos['x0']) ** 2 / sigma_x ** 2 + (yv - pos['y0']) ** 2 / sigma_y ** 2)) # add noise signal += 0.3 * numpy.random.random(size=signal.shape) # random walk of center of peak ('drift') pos['x0'] += 0.05 * (numpy.random.random() - 0.5) pos['y0'] += 0.05 * (numpy.random.random() - 0.5) # If previous frame was not added to the plot yet, skip this one if self.future_result is None or self.future_result.done(): # plot the data asynchronously, and # keep a reference to the `future` object self.future_result = concurrent.submitToQtMainThread( self.plot2d.addImage, signal, resetzoom=False)
def _threadedTest(self): """Function run in a thread for the tests""" value1, value2 = 0, 1 future = concurrent.submitToQtMainThread(self._task, value1, value2=value2) wait([future], 3) self.assertTrue(future.done()) self.assertEqual(future.result(1), (value1, value2)) self.assertIsNone(future.exception(1)) future = concurrent.submitToQtMainThread(self._taskWithException) wait([future], 3) self.assertTrue(future.done()) with self.assertRaises(RuntimeError): future.result(1) self.assertIsInstance(future.exception(1), RuntimeError)
def signalReceived(self, *args): """Received signal. Cancel previous timer and store args to be forwarded later.""" if self.blockSignal: return self.args = args if self.rateLimit == 0: concurrent.submitToQtMainThread(self.timer.stop) concurrent.submitToQtMainThread(self.timer.start, (self.delay * 1000) + 1) else: now = time() if self.lastFlushTime is None: leakTime = 0 else: lastFlush = self.lastFlushTime leakTime = max(0, (lastFlush + (1.0 / self.rateLimit)) - now) concurrent.submitToQtMainThread(self.timer.stop) concurrent.submitToQtMainThread( self.timer.start, (min(leakTime, self.delay) * 1000) + 1)
def run(self): while self.running: time.sleep(0.05) icon = self.animated_icon.currentIcon() self.future_result = concurrent.submitToQtMainThread( self._label.setPixmap, icon.pixmap(30, state=qt.QIcon.On))
def createSignal(*args, **kwargs): method = getattr(self.__target, name) result = concurrent.submitToQtMainThread(method, *args, **kwargs) return result