def select(self, timeout=None): self._context.acquire() try: if timeout is None: self._context.iteration(True) elif timeout <= 0: self._context.iteration(False) else: # Schedule fake callback that will trigger an event and cause the loop to terminate # after the given number of seconds handle = GLibHandle( loop=self, source=GLib.Timeout(timeout * 1000), repeat=False, callback=lambda: None, args=(), ) try: self._context.iteration(True) finally: handle.cancel() return ( ) # Available events are dispatched immediately and not returned finally: self._context.release()
def call_later(self, delay, callback, *args): return GLibHandle( loop=self, source=GLib.Timeout(delay*1000) if delay > 0 else GLib.Idle(), repeat=False, callback=callback, args=args)
def call_later(self, delay, callback, *args): if delay <= 0: return self.call_soon(callback, *args) else: return GLibHandle(self, GLib.Timeout(delay * 1000 if delay > 0 else 0), False, callback, args)
def source(self, func): timeout = self.timeout if timeout > 0: s = GLib.Timeout(timeout) else: s = GLib.Idle() s.set_callback(func) s.priority = self.priority return s
def _timeout_add(self, delay, callback, args, context=None, frame=None) -> 'GLibSourceHandle': # GLib.Timeout expects milliseconds. source = GLib.Timeout(delay * 1000) return self._schedule_callback(source, callback, args, context, frame)
def call_later(self, delay, callback, *args): self._check_not_coroutine(callback, 'call_later') return GLibHandle( loop=self, source=GLib.Timeout(delay * 1000) if delay > 0 else GLib.Idle(), repeat=False, callback=callback, args=args)
def _invoke(self, delay, callback, args): event = GLib.Timeout(delay * 1000) if delay > 0 else GLib.Idle() self.pending_events.add(event) def fire(context): self.pending_events.remove(event) event.destroy() callback(*args) event.set_callback(fire, None) event.attach(self._context)
def test_priority(self): s = GLib.Idle() self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT_IDLE) s.priority = GLib.PRIORITY_HIGH self.assertEqual(s.priority, GLib.PRIORITY_HIGH) s = GLib.Idle(GLib.PRIORITY_LOW) self.assertEqual(s.priority, GLib.PRIORITY_LOW) s = GLib.Timeout(1, GLib.PRIORITY_LOW) self.assertEqual(s.priority, GLib.PRIORITY_LOW) s = GLib.Source() self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT)
def _schedule_dispatch(self): if not self._ready or self._wakeup is not None: return def wakeup_cb(self): self._dispatch() if self._ready: return True else: self._wakeup.destroy() self._wakeup = None return False self._wakeup = GLib.Timeout(0) self._wakeup.set_callback(wakeup_cb, self) self._wakeup.attach(self._context)
def setup_timeout(self, loop): timeout = GLib.Timeout(500) timeout.set_callback(self.timeout_callback, loop) timeout.attach()
def test_out_of_scope_before_dispatch(self): # https://bugzilla.gnome.org/show_bug.cgi?id=504337 GLib.Timeout(20) GLib.Idle()
def test504337(self): GLib.Timeout(20) GLib.Idle()