class IdleAddHandler(QObject): signal = Signal(object) def __init__(self): QObject.__init__(self) self.main_thread_id = QThread.currentThreadId() self.signal.connect(self.run_func) def run_func(self, func): assert QThread.currentThreadId() == self.main_thread_id, \ ("Running in %s, not %s" % (str(QThread.currentThreadId()), str(self.main_thread_id))) func() def idle_add(self, func, *args): def doit(): try: func(*args) except Exception, e: logger.exception("Running %s%s: %s", func, str(tuple(args)), str(e)) if QThread.currentThreadId() == self.main_thread_id: # If we emit the signal in the main thread, # then the function will be run immediately. # Instead, use a single shot timer with a 0 # timeout: this will run the function when the # event loop next iterates. QTimer.singleShot(0, doit) else: self.signal.emit(doit)
def run_func(self, func): assert QThread.currentThreadId() == self.main_thread_id, \ ("Running in %s, not %s" % (str(QThread.currentThreadId()), str(self.main_thread_id))) func()
def __init__(self): QObject.__init__(self) self.main_thread_id = QThread.currentThreadId() self.signal.connect(self.run_func)
def testIntConversion(self): i = 0 h = QThread.currentThreadId() i = 0 + int(h) self.assertEqual(i, int(h))