def quit(self): if not self.exiting: self.exiting = True self.set_logging_level(logging.DEBUG) self.log.info("Exiting!") # finish all running instrument initialization threads for init_ctrl in self.instrument_init_controllers: if init_ctrl.isRunning(): init_ctrl.quit() init_ctrl.wait() # finish all running instrument controller threads for instr_ctrl in self.instrument_controllers: if instr_ctrl.isRunning(): instr_ctrl.quit() instr_ctrl.wait() # finish the data recorder thread if running if self.recorder.isRunning(): self.log.debug("Closing data recorder...") self.recorder.quit() self.recorder.wait() # finish notifier thread if self.notifier.isRunning(): self.log.debug("Closing notifier...") self.notifier.quit() self.notifier.wait() self.log.info("Goodbye!") QCoreApplication.quit()
def readStdin(self): line = sys.stdin.readline().strip() if line.lower() == "quit": QCoreApplication.quit() elif line.lower() == "reset": log.info("Resetting server.") self.colors = self.predefinedColors self.sm.reset() self.resetting.emit() elif line.lower() == "start": if not self.sm.next(Action.StartGame): log.warning("Need more players to start.")
class GateTest(QObject): def setUp(self): self.app = QCoreApplication(sys.argv) self._occurrences = Counter() def _increment(self, _, __): self._occurrences[self.sender()] += 1 for limit in self._limit: if self._occurrences == limit: self.app.quit() break def _wait(self): self._limit = [Counter(l) for l in self._limit] if self._limit[0]: self.app.exec_()
def sigint_handler(*args): QCoreApplication.quit()
class Qt4Reactor(PosixReactorBase): """ Qt4-based reactor. """ implements(IReactorFDSet) _timer = None def __init__(self): self._reads = {} self._writes = {} self._timer = QTimer() self._timer.setSingleShot(True) if QCoreApplication.startingUp(): self.qApp = QCoreApplication([]) self._ownApp = True else: self.qApp = QCoreApplication.instance() self._ownApp = False self._blockApp = None self._readWriteQ = [] PosixReactorBase.__init__(self) def addReader(self, reader): if not reader in self._reads: self._reads[reader] = TwistedSocketNotifier(self, reader, QSocketNotifier.Read) def addWriter(self, writer): if not writer in self._writes: self._writes[writer] = TwistedSocketNotifier(self, writer, QSocketNotifier.Write) def removeReader(self, reader): if reader in self._reads: self._reads.pop(reader).shutdown() def removeWriter(self, writer): if writer in self._writes: self._writes.pop(writer).shutdown() def removeAll(self): return self._removeAll(self._reads, self._writes) def getReaders(self): return self._reads.keys() def getWriters(self): return self._writes.keys() def callLater(self, howlong, *args, **kargs): rval = super(Qt4Reactor, self).callLater(howlong, *args, **kargs) self.reactorInvocation() return rval def crash(self): super(Qt4Reactor, self).crash() def iterate(self, delay=0.0): t = self.running self.running = True self._timer.stop() try: if delay == 0.0: self.reactorInvokePrivate() self._timer.stop() else: endTime = delay + time.time() self.reactorInvokePrivate() while True: t = endTime - time.time() if t <= 0.0: return self.qApp.processEvents(QEventLoop.AllEvents | QEventLoop.WaitForMoreEvents, int(t * 1010)) finally: self.running = t def addReadWrite(self, t): self._readWriteQ.append(t) def runReturn(self, installSignalHandlers=True): self._timer.timeout.connect(self.reactorInvokePrivate) self.startRunning(installSignalHandlers=installSignalHandlers) self._timer.start(0) def run(self, installSignalHandlers=True): try: if self._ownApp: self._blockApp = self.qApp else: self._blockApp = fakeApplication() self.runReturn(installSignalHandlers) self._blockApp.exec_() finally: self._timer.stop() def reactorInvocation(self): self._timer.setInterval(0) def reactorInvokePrivate(self): if not self.running: self.qApp.quit() self.runUntilCurrent() t = self.timeout() if t is None: t = 101 else: t = 1010 * min(t, 0.1) self._timer.setInterval(int(t)) self.qApp.processEvents() self._timer.start() def doIteration(self): assert False, "doIteration is invalid call"
class QTReactor(PosixReactorBase): """ Qt based reactor. """ implements(IReactorFDSet) _timer = None def __init__(self): self._reads = {} self._writes = {} self._timer=QTimer() self._timer.setSingleShot(True) if QCoreApplication.startingUp(): self.qApp=QCoreApplication([]) self._ownApp=True else: self.qApp = QCoreApplication.instance() self._ownApp=False self._blockApp = None self._readWriteQ=[] """ some debugging instrumentation """ self._doSomethingCount=0 PosixReactorBase.__init__(self) def addReader(self, reader): if not reader in self._reads: self._reads[reader] = TwistedSocketNotifier(self, reader, QSocketNotifier.Read) def addWriter(self, writer): if not writer in self._writes: self._writes[writer] = TwistedSocketNotifier(self, writer, QSocketNotifier.Write) def removeReader(self, reader): if reader in self._reads: #self._reads[reader].shutdown() #del self._reads[reader] self._reads.pop(reader).shutdown() def removeWriter(self, writer): if writer in self._writes: self._writes[writer].shutdown() #del self._writes[writer] self._writes.pop(writer) def removeAll(self): return self._removeAll(self._reads, self._writes) def getReaders(self): return self._reads.keys() def getWriters(self): return self._writes.keys() def callLater(self,howlong, *args, **kargs): rval = super(QTReactor,self).callLater(howlong, *args, **kargs) self.reactorInvocation() return rval def crash(self): super(QTReactor,self).crash() def iterate(self,delay=0.0): t=self.running # not sure I entirely get the state of running self.running=True self._timer.stop() # in case its not (rare?) try: if delay == 0.0: self.reactorInvokePrivate() self._timer.stop() # supports multiple invocations else: endTime = delay + time.time() self.reactorInvokePrivate() while True: t = endTime - time.time() if t <= 0.0: return self.qApp.processEvents(QEventLoop.AllEvents | QEventLoop.WaitForMoreEvents,t*1010) finally: self.running=t def addReadWrite(self,t): self._readWriteQ.append(t) def runReturn(self, installSignalHandlers=True): QObject.connect(self._timer, SIGNAL("timeout()"), self.reactorInvokePrivate) self.startRunning(installSignalHandlers=installSignalHandlers) self._timer.start(0) def run(self, installSignalHandlers=True): try: if self._ownApp: self._blockApp=self.qApp else: self._blockApp = fakeApplication() self.runReturn(installSignalHandlers) self._blockApp.exec_() finally: self._timer.stop() # should already be stopped def reactorInvocation(self): self._timer.setInterval(0) def reactorInvokePrivate(self): if not self.running: if self._blockApp is None: # Andy's fix for Ctrl-C quit self.qApp.quit() else: self._blockApp.quit() self._doSomethingCount += 1 self.runUntilCurrent() t = self.timeout() if t is None: t=0.1 else: t = min(t,0.1) self._timer.setInterval(int(t*1010)) self.qApp.processEvents() # could change interval self._timer.start() def doIteration(self): assert False, "doiteration is invalid call"
def sigint_handler(*args): QCoreApplication.quit()
def uncaught(type, value, traceback): print type, value, traceback QCoreApplication.quit()
def handleReply(self, reply): print reply.readAll() print reply.error() QCoreApplication.quit()
class Qt4Reactor(QObject, PosixReactorBase): implements(IReactorFDSet) # Note: @pyqtSignature is necessary for QMetaObject.invokeMethod() to work. # @pyqtSlot is not used for compatibility with PyQt versions before 4.5. # Note: Twisted 10.0 and earlier is unable to wake up Qt event loop on SIGCHLD. # This does not pose a problem for GUI apps and was fixed in Twisted 10.1 so no workaround here. def __init__(self): QObject.__init__(self) self._readers = {} self._writers = {} self._timer = QTimer() self._timer.setSingleShot(True) self.connect(self._timer, SIGNAL("timeout()"), self._timerSlot) self._eventLoop = QCoreApplication.instance() if self._eventLoop is None: # create dummy application for testing self._eventLoop = QCoreApplication([]) PosixReactorBase.__init__(self) # goes last, because it calls addReader def _scheduleSimulation(self, timeout=0): if timeout is None: self._timer.stop() else: self._timer.start(timeout * 1000) @pyqtSignature("") def _timerSlot(self): self.runUntilCurrent() self._scheduleSimulation(self.timeout()) def _addNotifier(self, descriptor, descmap, type): if descriptor not in descmap: fd = descriptor.fileno() if fd == -1: raise RuntimeError("Invalid file descriptor") notifier = QSocketNotifier(fd, type) descmap[descriptor] = notifier notifier._descriptor = descriptor self.connect(notifier, SIGNAL("activated(int)"), self._notifierSlot) notifier.setEnabled(True) def _removeNotifier(self, descriptor, descmap): notifier = descmap.pop(descriptor, None) if notifier is not None: notifier.setEnabled(False) self.disconnect(notifier, SIGNAL("activated(int)"), self._notifierSlot) notifier._descriptor = None notifier.deleteLater() @pyqtSignature("") def _notifierSlot(self): # Note: in some tests on Ubuntu 10.4 (PyQt 4.7.2) new-style signal connections result # in NULL QObject.sender() here. Old style signal connections do not have this problem. notifier = self.sender() notifier.setEnabled(False) descriptor = notifier._descriptor if descriptor is None: return isRead = notifier.type() == notifier.Read try: if isRead: why = descriptor.doRead() else: why = descriptor.doWrite() except: log.err() why = sys.exc_info()[1] if descriptor.fileno() != notifier.socket(): why = ConnectionFdescWentAway("Filedescriptor went away") if why: self._disconnectSelectable(descriptor, why, isRead) elif notifier._descriptor: # check if not disconnected in doRead/doWrite notifier.setEnabled(True) # Twisted (FTP) expects due timed events to be delivered after each IO event, # so that invoking callLater(0, fn) from IO callback results in fn() being called before # the next IO callback. self.runUntilCurrent() def addReader(self, reader): self._addNotifier(reader, self._readers, QSocketNotifier.Read) def addWriter(self, writer): self._addNotifier(writer, self._writers, QSocketNotifier.Write) def removeReader(self, reader): self._removeNotifier(reader, self._readers) def removeWriter(self, writer): self._removeNotifier(writer, self._writers) def removeAll(self): return self._removeAll(self._readers, self._writers) def getReaders(self): return self._readers.keys() def getWriters(self): return self._writers.keys() def callLater(self, *args, **kwargs): result = PosixReactorBase.callLater(self, *args, **kwargs) self._scheduleSimulation() return result def _moveCallLaterSooner(self, tple): result = PosixReactorBase._moveCallLaterSooner(self, tple) self._scheduleSimulate() return result def doIteration(self, delay): # Note: some tests fail on Ubuntu 10.4 when processEvents is called with zero delay. # Setting the minimal delay to something > 0 or or calling processEvents() twice # fixes the problem. self._eventLoop.processEvents(QEventLoop.AllEvents, max(1, delay) * 1000) def mainLoop(self): self._eventLoop.exec_() def stop(self): PosixReactorBase.stop(self) self._scheduleSimulation() def crash(self): PosixReactorBase.crash(self) self._eventLoop.quit() if sys.platform == "win32": # On Windows, we can wake up by simply posting a message to the main loop. # Other systems call wakeUp() from unix signal handlers (which are forbidden from making # any Qt calls) and have to use the default pipe based waker. def installWaker(self): pass def wakeUp(self): QMetaObject.invokeMethod(self, "_timerSlot", Qt.QueuedConnection)
class QTReactor(PosixReactorBase): """ Qt based reactor. """ implements(IReactorFDSet) _timer = None def __init__(self): self._reads = {} self._writes = {} self._timer = QTimer() self._timer.setSingleShot(True) if QCoreApplication.startingUp(): self.qApp = QCoreApplication([]) self._ownApp = True else: self.qApp = QCoreApplication.instance() self._ownApp = False self._blockApp = None self._readWriteQ = [] """ some debugging instrumentation """ self._doSomethingCount = 0 PosixReactorBase.__init__(self) def addReader(self, reader): if not reader in self._reads: self._reads[reader] = TwistedSocketNotifier( self, reader, QSocketNotifier.Read) def addWriter(self, writer): if not writer in self._writes: self._writes[writer] = TwistedSocketNotifier( self, writer, QSocketNotifier.Write) def removeReader(self, reader): if reader in self._reads: #self._reads[reader].shutdown() #del self._reads[reader] self._reads.pop(reader).shutdown() def removeWriter(self, writer): if writer in self._writes: self._writes[writer].shutdown() #del self._writes[writer] self._writes.pop(writer) def removeAll(self): return self._removeAll(self._reads, self._writes) def getReaders(self): return self._reads.keys() def getWriters(self): return self._writes.keys() def callLater(self, howlong, *args, **kargs): rval = super(QTReactor, self).callLater(howlong, *args, **kargs) self.reactorInvocation() return rval def crash(self): super(QTReactor, self).crash() def iterate(self, delay=0.0): t = self.running # not sure I entirely get the state of running self.running = True self._timer.stop() # in case its not (rare?) try: if delay == 0.0: self.reactorInvokePrivate() self._timer.stop() # supports multiple invocations else: endTime = delay + time.time() self.reactorInvokePrivate() while True: t = endTime - time.time() if t <= 0.0: return self.qApp.processEvents( QEventLoop.AllEvents | QEventLoop.WaitForMoreEvents, t * 1010) finally: self.running = t def addReadWrite(self, t): self._readWriteQ.append(t) def runReturn(self, installSignalHandlers=True): QObject.connect(self._timer, SIGNAL("timeout()"), self.reactorInvokePrivate) self.startRunning(installSignalHandlers=installSignalHandlers) self._timer.start(0) def run(self, installSignalHandlers=True): try: if self._ownApp: self._blockApp = self.qApp else: self._blockApp = fakeApplication() self.runReturn(installSignalHandlers) self._blockApp.exec_() finally: self._timer.stop() # should already be stopped def reactorInvocation(self): self._timer.setInterval(0) def reactorInvokePrivate(self): if not self.running: if self._blockApp is None: # Andy's fix for Ctrl-C quit self.qApp.quit() else: self._blockApp.quit() self._doSomethingCount += 1 self.runUntilCurrent() t = self.timeout() if t is None: t = 0.1 else: t = min(t, 0.1) self._timer.setInterval(int(t * 1010)) self.qApp.processEvents() # could change interval self._timer.start() def doIteration(self): assert False, "doiteration is invalid call"
class Qt4Reactor(QObject, PosixReactorBase): implements(IReactorFDSet) # Note: @pyqtSignature is necessary for QMetaObject.invokeMethod() to work. # @pyqtSlot is not used for compatibility with PyQt versions before 4.5. # Note: Twisted 10.0 and earlier is unable to wake up Qt event loop on SIGCHLD. # This does not pose a problem for GUI apps and was fixed in Twisted 10.1 so no workaround here. def __init__(self): QObject.__init__(self) self._readers = {} self._writers = {} self._timer = QTimer() self._timer.setSingleShot(True) self.connect(self._timer, SIGNAL("timeout()"), self._timerSlot) self._eventLoop = QCoreApplication.instance() if self._eventLoop is None: # create dummy application for testing self._eventLoop = QCoreApplication([]) PosixReactorBase.__init__(self) # goes last, because it calls addReader def _scheduleSimulation(self, timeout=0): if timeout is None: self._timer.stop() else: self._timer.start(timeout * 1000) @pyqtSignature("") def _timerSlot(self): self.runUntilCurrent() self._scheduleSimulation(self.timeout()) def _addNotifier(self, descriptor, descmap, type): if descriptor not in descmap: fd = descriptor.fileno() if fd == -1: raise RuntimeError("Invalid file descriptor") notifier = QSocketNotifier(fd, type) descmap[descriptor] = notifier notifier._descriptor = descriptor self.connect(notifier, SIGNAL("activated(int)"), self._notifierSlot) notifier.setEnabled(True) def _removeNotifier(self, descriptor, descmap): notifier = descmap.pop(descriptor, None) if notifier is not None: notifier.setEnabled(False) self.disconnect(notifier, SIGNAL("activated(int)"), self._notifierSlot) notifier._descriptor = None notifier.deleteLater() @pyqtSignature("") def _notifierSlot(self): # Note: in some tests on Ubuntu 10.4 (PyQt 4.7.2) new-style signal connections result # in NULL QObject.sender() here. Old style signal connections do not have this problem. notifier = self.sender() notifier.setEnabled(False) descriptor = notifier._descriptor if descriptor is None: return isRead = (notifier.type() == notifier.Read) try: if isRead: why = descriptor.doRead() else: why = descriptor.doWrite() except: log.err() why = sys.exc_info()[1] if descriptor.fileno() != notifier.socket(): why = ConnectionFdescWentAway('Filedescriptor went away') if why: self._disconnectSelectable(descriptor, why, isRead) elif notifier._descriptor: # check if not disconnected in doRead/doWrite notifier.setEnabled(True) # Twisted (FTP) expects due timed events to be delivered after each IO event, # so that invoking callLater(0, fn) from IO callback results in fn() being called before # the next IO callback. self.runUntilCurrent() def addReader(self, reader): self._addNotifier(reader, self._readers, QSocketNotifier.Read) def addWriter(self, writer): self._addNotifier(writer, self._writers, QSocketNotifier.Write) def removeReader(self, reader): self._removeNotifier(reader, self._readers) def removeWriter(self, writer): self._removeNotifier(writer, self._writers) def removeAll(self): return self._removeAll(self._readers, self._writers) def getReaders(self): return self._readers.keys() def getWriters(self): return self._writers.keys() def callLater(self, *args, **kwargs): result = PosixReactorBase.callLater(self, *args, **kwargs) self._scheduleSimulation() return result def _moveCallLaterSooner(self, tple): result = PosixReactorBase._moveCallLaterSooner(self, tple) self._scheduleSimulate() return result def doIteration(self, delay): # Note: some tests fail on Ubuntu 10.4 when processEvents is called with zero delay. # Setting the minimal delay to something > 0 or or calling processEvents() twice # fixes the problem. self._eventLoop.processEvents(QEventLoop.AllEvents, max(1, delay) * 1000) def mainLoop(self): self._eventLoop.exec_() def stop(self): PosixReactorBase.stop(self) self._scheduleSimulation() def crash(self): PosixReactorBase.crash(self) self._eventLoop.quit() if sys.platform == "win32": # On Windows, we can wake up by simply posting a message to the main loop. # Other systems call wakeUp() from unix signal handlers (which are forbidden from making # any Qt calls) and have to use the default pipe based waker. def installWaker(self): pass def wakeUp(self): QMetaObject.invokeMethod(self, "_timerSlot", Qt.QueuedConnection)
def uncaught(type, value, traceback): print type, value, traceback QCoreApplication.quit()
def closeEvent(self,event): self.client_core.save_configuration() QCoreApplication.quit()