def stop(self): self.send(removeReader(self, self.listener), "_selectorSignal") # for i in xrange(100): yield 1 self.send(shutdown(), "_selectorShutdownSignal") self.listener.close() # Ensure we close the server socket. Only really likely to # be called from the scheduler shutting down due to a stop. super(TCPServer,self).stop()
def main(self): selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices(self.tracker) if newSelector: newSelector.activate() self.link((self, "_selectorSignal"),selectorService) self.link((self, "_selectorShutdownSignal"),selectorShutdownService) self.selectorService = selectorService self.selectorShutdownService = selectorShutdownService self.send(newReader(self, ((self, "newconnection"), self.listener)), "_selectorSignal") yield 1 while 1: if not self.anyReady(): self.pause() if self.anyClosedSockets(): for i in xrange(10): yield 1 self.handleNewConnection() # Data ready means that we have a connection waiting. if self.dataReady("control"): data = self.recv("control") if isinstance(data, serverShutdown): break yield 1 self.send(removeReader(self, self.listener), "_selectorSignal") # for i in xrange(100): yield 1 self.send(shutdown(), "_selectorShutdownSignal")
def main(self): if self.listener is None: self.send(shutdownMicroprocess, 'signal') # FIXME: Should probably be producerFinished. # FIXME: (ie advisory that I've finished, rather than # FIXME: demand next component to shutdown) yield 1 return # NOTE: Change from suggested fix. (Simplifies code logic/makes diff smaller) selectorService, selectorShutdownService, newSelector = Selector.getSelectorServices(self.tracker) if newSelector: newSelector.activate() self.link((self, "_selectorSignal"),selectorService) self.link((self, "_selectorShutdownSignal"),selectorShutdownService) self.selectorService = selectorService self.selectorShutdownService = selectorShutdownService self.send(newReader(self, ((self, "newconnection"), self.listener)), "_selectorSignal") yield 1 while 1: if not self.anyReady(): self.pause() yield 1 if self.anyClosedSockets(): for i in xrange(10): yield 1 self.handleNewConnection() # Data ready means that we have a connection waiting. if self.dataReady("control"): data = self.recv("control") if isinstance(data, serverShutdown): break yield 1 self.send(removeReader(self, self.listener), "_selectorSignal") self.send(shutdown(), "_selectorShutdownSignal")
def main(self): # print ("self.selectorService", self, self.selectorService) self.link((self, "_selectorSignal"), self.selectorService) # self.selectorService ... self.sending = True self.receiving = True self.connectionRECVLive = True self.connectionRECVLive = True self.connectionSENDLive = True # Print( "CSA Activated") while self.connectionRECVLive and self.connectionSENDLive: # Note, this means half close == close yield 1 if self.dataReady("makessl"): # print ("****************************************************** Making SSL ******************************************************") # Print( "CSA Made SSL") self.recv('makessl') self.send(removeReader(self, self.socket), "_selectorSignal") self.send(removeWriter(self, self.socket), "_selectorSignal") # We need to block to allow the handshake to complete self.socket.setblocking(True) self.socket = SSLSocket(self.socket) self.isSSL = True self.socket.setblocking(False) self.send(newReader(self, ((self, "ReadReady"), self.socket)), "_selectorSignal") self.send(newWriter(self, ((self, "SendReady"), self.socket)), "_selectorSignal") self.send('', 'sslready') # print ("****************************************************** SSL IS READY ******************************************************") yield 1 self.checkSocketStatus() # To be written self.handleControl( ) # Check for producerFinished message in "control" and shutdown in response if self.sending: self.flushSendQueue() if self.receiving: self.handleReceive() if not self.canDoSomething(): self.pause() # self.passOnShutdown() # Print( "Stopping...") self.stop() # NOTE: the creator of this CSA is responsible for removing it from the selector # print ("") # print ("------------------------------------------------------------------------------------") # print ("DROPPED OFF THE END OF THE GENERATOR",self.socket) self.socket = None # print (self.__dict__) # print (self.postoffice) # print ([str(x) for x in self.postoffice.linkages]) for linkage in self.postoffice.linkages: self.unlink(thelinkage=linkage)
def main(self): """Main loop""" selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices( self.tracker ) # get a reference to a Selector component so we do not have to poll the file descriptor for readiness if newSelectorService: newSelectorService.activate() self.addChildren(newSelectorService) self.link((self, "_selectorask"), selectorService) try: self.fd = self.openFile(self.filename) except Exception: e = sys.exc_info()[1] print(e) return self.makeNonBlocking(self.fd) self.selectorWait(self.fd) self.done = False waiting = True while not self.done: #print ("main") yield 1 # we use inbox just to wake us up while self.dataReady("inbox"): msg = self.recv("inbox") # if we should send some more if we can if self.dataReady("_selectorready"): #print ("selector is ready") waiting = False msg = self.recv("_selectorready") if not waiting: readsomething = False while len(self.outboxes["outbox"] ) < self.maxqueue and self.tryReadChunk(self.fd): readsomething = True pass if readsomething: self.selectorWait(self.fd) waiting = True if not self.done: self.pause() self.send(removeReader(self, self.fd), '_selectorask') os.close(self.fd) self.send(producerFinished(self), "signal") self.debug("IntelligentFileReader terminated")
def main(self): selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) dataPending = "" waitingToStop=False self.shutdownMsg = None try: while 1: while dataPending: self.checkShutdown() try: self.send(dataPending,"outbox") dataPending="" except noSpaceInBox: self.pause() yield 1 while not dataPending: try: #dataPending=self.fh.read(self.maxReadChunkSize) dataPending = os.read(self.fh.fileno(), self.maxReadChunkSize) if dataPending=="": raise "STOP" except OSError,IOError: # no data available yet, need to wait if self.checkShutdown(): raise "STOP" if self.dataReady("ready"): self.recv("ready") else: self.send(newReader(self,((self, "ready"), self.fh)), "selector") while not self.dataReady("ready") and not self.checkShutdown(): self.pause() yield 1 if self.dataReady("ready"): self.recv("ready") except "STOP": pass # ordered to shutdown! self.send(removeReader(self,(self.fh)), "selector") try: self.fh.close() except: pass yield 1 yield 1 if not self.shutdownMsg: self.send(producerFinished(self), "signal") else: self.send(self.shutdownMsg,"signal")
def main(self): selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) dataPending = "" waitingToStop=False self.shutdownMsg = None try: while 1: while dataPending: self.checkShutdown() try: self.send(dataPending,"outbox") dataPending="" except noSpaceInBox: self.pause() yield 1 while not dataPending: try: #dataPending=self.fh.read(self.maxReadChunkSize) dataPending = os.read(self.fh.fileno(), self.maxReadChunkSize) if dataPending=="": raise UserWarning( "STOP" ) except OSError,IOError: # no data available yet, need to wait if self.checkShutdown(): raise UserWarning( "STOP" ) if self.dataReady("ready"): self.recv("ready") else: self.send(newReader(self,((self, "ready"), self.fh)), "selector") while not self.dataReady("ready") and not self.checkShutdown(): self.pause() yield 1 if self.dataReady("ready"): self.recv("ready") except "STOP": pass # ordered to shutdown! self.send(removeReader(self,(self.fh)), "selector") try: self.fh.close() except: pass yield 1 yield 1 if not self.shutdownMsg: self.send(producerFinished(self), "signal") else: self.send(self.shutdownMsg,"signal")
def stop(self): if self.listener is not None: self.send(removeReader(self, self.listener), "_selectorSignal") self.send(shutdown(), "_selectorShutdownSignal") self.listener.close() # Ensure we close the server socket. Only really likely to # be called from the scheduler shutting down due to a stop. self.listener = None super(TCPServer,self).stop()
def main(self): """Main loop.""" for v in self.runClient(): yield v if (self.sock is not None) and (self.CSA is not None): self.send(removeReader(self.CSA, self.sock), "_selectorSignal") self.send(removeWriter(self.CSA, self.sock), "_selectorSignal")
def main(self): # print ("self.selectorService", self, self.selectorService) self.link((self, "_selectorSignal"), self.selectorService) # self.selectorService ... self.sending = True self.receiving = True self.connectionRECVLive = True self.connectionRECVLive = True self.connectionSENDLive = True # Print( "CSA Activated") while self.connectionRECVLive and self.connectionSENDLive: # Note, this means half close == close yield 1 if self.dataReady("makessl"): # print ("****************************************************** Making SSL ******************************************************") # Print( "CSA Made SSL") self.recv('makessl') self.send(removeReader(self, self.socket), "_selectorSignal") self.send(removeWriter(self, self.socket), "_selectorSignal") # We need to block to allow the handshake to complete self.socket.setblocking(True) self.socket = SSLSocket(self.socket) self.isSSL = True self.socket.setblocking(False) self.send(newReader(self, ((self, "ReadReady"), self.socket)), "_selectorSignal") self.send(newWriter(self, ((self, "SendReady"), self.socket)), "_selectorSignal") self.send('', 'sslready') # print ("****************************************************** SSL IS READY ******************************************************") yield 1 self.checkSocketStatus() # To be written self.handleControl() # Check for producerFinished message in "control" and shutdown in response if self.sending: self.flushSendQueue() if self.receiving: self.handleReceive() if not self.canDoSomething(): self.pause() # self.passOnShutdown() # Print( "Stopping...") self.stop() # NOTE: the creator of this CSA is responsible for removing it from the selector # print ("") # print ("------------------------------------------------------------------------------------") # print ("DROPPED OFF THE END OF THE GENERATOR",self.socket) self.socket=None # print (self.__dict__) # print (self.postoffice) # print ([str(x) for x in self.postoffice.linkages]) for linkage in self.postoffice.linkages: self.unlink(thelinkage=linkage)
def main(self): """Main loop""" selectorService, selectorShutdownService, newSelectorService = Selector.getSelectorServices(self.tracker) # get a reference to a Selector component so we do not have to poll the file descriptor for readiness if newSelectorService: newSelectorService.activate() self.addChildren(newSelectorService) self.link((self, "_selectorask"), selectorService) try: self.fd = self.openFile(self.filename) except Exception: e = sys.exc_info()[1] print (e) return self.makeNonBlocking(self.fd) self.selectorWait(self.fd) self.done = False waiting = True while not self.done: #print ("main") yield 1 # we use inbox just to wake us up while self.dataReady("inbox"): msg = self.recv("inbox") # if we should send some more if we can if self.dataReady("_selectorready"): #print ("selector is ready") waiting = False msg = self.recv("_selectorready") if not waiting: readsomething = False while len(self.outboxes["outbox"]) < self.maxqueue and self.tryReadChunk(self.fd): readsomething = True pass if readsomething: self.selectorWait(self.fd) waiting = True if not self.done: self.pause() self.send(removeReader(self, self.fd), '_selectorask') os.close(self.fd) self.send(producerFinished(self), "signal") self.debug("IntelligentFileReader terminated")
def main(self): """Main loop.""" # wait before connecting waitUntil = time.time() + self.delay while time.time() < waitUntil: yield 1 for v in self.runClient(): yield v if (self.sock is not None) and (self.CSA is not None): self.send(removeReader(self.CSA, self.sock), "_selectorSignal") self.send(removeWriter(self.CSA, self.sock), "_selectorSignal")
def main(self): """ Main loop """ if not self.safeBind(self.local): self.send(shutdownMicroprocess, "signal") # FIXME: Should probably be producer Finished. yield 1 return self.sock.setblocking(0) self.setupSelector() yield 1 self.send(newWriter(self, ((self, "writeReady"), self.sock)), "_selectorSignal") self.send(newReader(self, ((self, "readReady"), self.sock)), "_selectorSignal") while 1: if self.dataReady("control"): msg = self.recv("control") if isinstance(msg, shutdownMicroprocess): self.send(msg, "signal") break if self.dataReady("target"): self.remote = self.recv("target") if self.dataReady("writeReady"): self.recv("writeReady") self.sending = True if self.dataReady("readReady"): self.recv("readReady") self.receiving = True if self.sending: self.sendLoop(self.remote) if self.receiving: self.recvLoop() if (not self.anyReady() or not (self.sending and len(self.sendBuffer[0]) != 0)): self.pause() yield 1 self.send(removeReader(self, self.sock), "_selectorSignal") self.send(removeWriter(self, self.sock), "_selectorSignal") yield 1 self.sock.close()
def stop(self): """Stop method provided to allow the scheduler to kill TCPClient connections cleanly if necessary. (Only rarely, if ever, needed - you are not expected to call this yourself)""" try: self.sock.shutdown(2) except: pass # Well, we tried. try: self.sock.close() except: pass # Well, we tried. self.send(producerFinished(self, self.howDied), "signal") if (self.sock is not None) and (self.CSA is not None): self.send(removeReader(self.CSA, self.sock), "_selectorSignal") self.send(removeWriter(self.CSA, self.sock), "_selectorSignal") self.send(producerFinished(), "signal") super(TCPClient, self).stop()
def stop(self): """Stop method provided to allow the scheduler to kill TCPClient connections cleanly if necessary. (Only rarely, if ever, needed - you are not expected to call this yourself)""" try: self.sock.shutdown(2) except: pass # Well, we tried. try: self.sock.close() except: pass # Well, we tried. self.send(producerFinished(self,self.howDied), "signal") if (self.sock is not None) and (self.CSA is not None): self.send(removeReader(self.CSA, self.sock), "_selectorSignal") self.send(removeWriter(self.CSA, self.sock), "_selectorSignal") self.send(producerFinished(),"signal") super(TCPClient, self).stop()
def test_RemoveReader_ResultsInReaderNoLongerBeingSelectedOrWiredIn(self): "main - Sending a remove reader message unwires/links a component, and also removes it's selectable from the readers list" MOCKSELECTORMODULE = MockSelect(results=[ ([], [], [] )]) SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() D = Axon.Component.component() dummyservice = (D, "inbox") S._deliver(newReader(S,( dummyservice, "LOOKINGFORTHIS") ),"notify") S._deliver(removeReader(S,"LOOKINGFORTHIS"),"notify") for i in xrange(100): S.next(); try: S.postoffice.next() except: pass self.assert_( len(D.inboxes["inbox"]) == 0 )
def main(self): """ Main loop """ if not self.safeBind(self.local): self.send(shutdownMicroprocess, "signal") # FIXME: Should probably be producer Finished. yield 1 return # FIXME: This should possibly deal with problems with setting the # socket non-blocking self.sock.setblocking(0) self.setupSelector() yield 1 self.send(newReader(self, ((self, "readReady"), self.sock)), "_selectorSignal") while 1: if self.dataReady("control"): msg = self.recv("control") if isinstance(msg, shutdownMicroprocess): self.send(msg, "signal") break if self.dataReady("readReady"): self.recv("readReady") self.receiving = True if self.receiving: self.recvLoop() if not self.anyReady(): self.pause() yield 1 self.send(removeReader(self, self.sock), "_selectorSignal") yield 1 try: self.sock.close() except: # In case the socket close fails for whatever reason pass self.sock = None
def test_RemoveReader_ResultsInReaderNoLongerBeingSelectedOrWiredIn(self): "main - Sending a remove reader message unwires/links a component, and also removes it's selectable from the readers list" MOCKSELECTORMODULE = MockSelect(results=[([], [], [])]) SELECTORMODULE.select = MOCKSELECTORMODULE S = Selector() S.activate() for i in xrange(100): S.next() D = Axon.Component.component() dummyservice = (D, "inbox") S._deliver(newReader(S, (dummyservice, "LOOKINGFORTHIS")), "notify") S._deliver(removeReader(S, "LOOKINGFORTHIS"), "notify") for i in xrange(100): S.next() try: S.postoffice.next() except: pass self.assert_(len(D.inboxes["inbox"]) == 0)
def closeSocket(self, shutdownMessage): """\ Respond to a socketShutdown message by closing the socket. Sends a removeReader and removeWriter message to the selectorComponent. Sends a shutdownCSA(self, theCSA) message to "protocolHandlerSignal" outbox. """ theComponent, ( sock, howdied) = shutdownMessage.caller, shutdownMessage.message shutdownMessage.caller, shutdownMessage.message = None, None shutdownMessage = None # print ("SOCKET HANDLERS BEFORE", self.socket_handlers) # print ("TCPServer.closeSocket", theComponent,(sock,howdied)) ### FIXME: Pass on how died as well in TCPServer! found = False for s in self.socket_handlers: if self.socket_handlers[s] == theComponent: found = True break if found: sock = s if sock is not None: theComponent = self.socket_handlers[sock] sock.close() if theComponent: # tell the selector about it shutting down self.send(removeReader(theComponent, sock), "_selectorSignal") self.send(removeWriter(theComponent, sock), "_selectorSignal") # tell protocol handlers self.send(_ki.shutdownCSA(self, theComponent), "protocolHandlerSignal") # "signal") # Delete the child component # print; print ("DELETING", theComponent) self.removeChild(theComponent) if sock: del self.socket_handlers[sock] sock = None
def stop(self): # Some of these are going to crash initially when stop is called # print ("I AM CALLED") # Print( "stopping") if self.socket is None: # SELF.STOP CALLED TWICE - possible under limited circumstances (crashes primarily) # Only want to call once though, so exit here. # Print( "Oh, hold on") return try: self.socket.shutdown(2) # Print( "socket.shutdown succeeded") except Exception: e = sys.exc_info()[1] # Explicitly silencing this because it is possible (but rare) that # the socket was already shutdown due to an earlier error. # Print( "socket.shutdown failed for some reason", e) pass try: # Print( "socket.close ...") self.socket.close() # Print( " ... succeeded") except Exception: e = sys.exc_info()[1] # Print( " ... failed") # Explicitly silencing this because it is possible (but rare) that # the socket was already closed due to an earlier error. pass sock = self.socket self.socket = None self.passOnShutdown() if (sock is not None): self.send(removeReader(self, sock), "_selectorSignal") self.send(removeWriter(self, sock), "_selectorSignal") sock = None super(ConnectedSocketAdapter, self).stop() self.stop = lambda : None # Make it rather hard to call us twice by mistake
def stop(self): # Some of these are going to crash initially when stop is called # print ("I AM CALLED") # Print( "stopping") if self.socket is None: # SELF.STOP CALLED TWICE - possible under limited circumstances (crashes primarily) # Only want to call once though, so exit here. # Print( "Oh, hold on") return try: self.socket.shutdown(2) # Print( "socket.shutdown succeeded") except Exception: e = sys.exc_info()[1] # Explicitly silencing this because it is possible (but rare) that # the socket was already shutdown due to an earlier error. # Print( "socket.shutdown failed for some reason", e) pass try: # Print( "socket.close ...") self.socket.close() # Print( " ... succeeded") except Exception: e = sys.exc_info()[1] # Print( " ... failed") # Explicitly silencing this because it is possible (but rare) that # the socket was already closed due to an earlier error. pass sock = self.socket self.socket = None self.passOnShutdown() if (sock is not None): self.send(removeReader(self, sock), "_selectorSignal") self.send(removeWriter(self, sock), "_selectorSignal") sock = None super(ConnectedSocketAdapter, self).stop() self.stop = lambda: None # Make it rather hard to call us twice by mistake
def closeSocket(self, shutdownMessage): """\ Respond to a socketShutdown message by closing the socket. Sends a removeReader and removeWriter message to the selectorComponent. Sends a shutdownCSA(self, theCSA) message to "protocolHandlerSignal" outbox. """ theComponent,(sock,howdied) = shutdownMessage.caller, shutdownMessage.message shutdownMessage.caller, shutdownMessage.message = None, None shutdownMessage = None # print "SOCKET HANDLERS BEFORE", self.socket_handlers # print "TCPServer.closeSocket", theComponent,(sock,howdied) ### FIXME: Pass on how died as well in TCPServer! found = False for s in self.socket_handlers: if self.socket_handlers[s]==theComponent: found = True break if found: sock = s if sock is not None: theComponent = self.socket_handlers[sock] sock.close() if theComponent: # tell the selector about it shutting down self.send(removeReader(theComponent, sock), "_selectorSignal") self.send(removeWriter(theComponent, sock), "_selectorSignal") # tell protocol handlers self.send(_ki.shutdownCSA(self, theComponent), "protocolHandlerSignal")# "signal") # Delete the child component # print; print "DELETING", theComponent self.removeChild(theComponent) if sock: del self.socket_handlers[sock] sock = None
def closeSocket(self, shutdownMessage): """\ Respond to a socketShutdown message by closing the socket. Sends a removeReader and removeWriter message to the selectorComponent. Sends a shutdownCSA(self, theCSA) message to "protocolHandlerSignal" outbox. """ theComponent,(sock,howdied) = shutdownMessage.caller, shutdownMessage.message ### FIXME: Pass on how died as well in TCPServer! theComponent = self.socket_handlers[sock] sock.close() # tell the selector about it shutting down self.send(removeReader(theComponent, sock), "_selectorSignal") self.send(removeWriter(theComponent, sock), "_selectorSignal") # self.send(removeReader(theComponent, theComponent.socket), "_selectorSignal") # self.send(removeWriter(theComponent, theComponent.socket), "_selectorSignal") # tell protocol handlers self.send(_ki.shutdownCSA(self, theComponent), "protocolHandlerSignal")# "signal") # Delete the child component self.removeChild(theComponent)
try: Y = os.read(x.stdout.fileno(), 10) while Y: self.send(Y, "outbox") Y = os.read(x.stdout.fileno(), 10) # \ #print self.name,"Mighty Floogly" except OSError, e: continue except: break yield 1 # remove now closed file handles from the selector, so it doesn't stay # upset self.send(removeReader(self, (x.stderr)), "selector") self.send(removeReader(self, (x.stdout)), "selector") self.send(removeWriter(self, (x.stdin)), "selector") # \ #print self.name,"sending shutdown" if not shutdownMessage: # \ #print self.name,"new signal" self.send(Axon.Ipc.producerFinished(), "signal") # \ #print self.name,"...sent" else: # \ #print self.name,"old signal" self.send(shutdownMessage, "signal")
def main(self): writeBuffer = [] shutdownMessage = False selectorService, selectorShutdownService, S = Selector.getSelectorServices(self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) # self.link((self, "selectorsignal"), (selectorShutdownService)) x = self.openSubprocess() self.send(newWriter(self,((self, "stdinready"), x.stdin)), "selector") self.send(newReader(self,((self, "stderrready"), x.stderr)), "selector") self.send(newReader(self,((self, "stdoutready"), x.stdout)), "selector") # Assume all ready stdin_ready = 1 stdout_ready = 1 stderr_ready = 1 exit_status = x.poll() # while x.poll() is None success = 0 while exit_status is None: exit_status = x.poll() if (not self.anyReady()) and not (stdin_ready + stdout_ready + stderr_ready): # \ #print (self.name,"Mighty Foo", stdin_ready, stdout_ready, stderr_ready, len(self.inboxes["inbox"]), len(writeBuffer)) self.pause() yield 1 continue while self.dataReady("inbox"): d = self.recv("inbox") writeBuffer.append(d) if self.dataReady("stdinready"): self.recv("stdinready") stdin_ready = 1 if self.dataReady("stdoutready"): self.recv("stdoutready") stdout_ready = 1 if self.dataReady("stderrready"): self.recv("stderrready") stderr_ready = 1 if len(writeBuffer)>10000: writeBuffer=writeBuffer[-10000:] if stdin_ready: while len(writeBuffer) >0: d = writeBuffer[0] # d = writeBuffer.pop(0) try: count = os.write(x.stdin.fileno(), d) writeBuffer.pop(0) success +=1 except OSError: e =sys.exc_info()[1] success -=1 # \ #print (self.name,"Mighty FooBar", len(self.inboxes["inbox"]), len(writeBuffer)) # Stdin wasn't ready. Let's send through a newWriter request # Want to wait stdin_ready = 0 writeBuffer=writeBuffer[len(writeBuffer)/2:] self.send(newWriter(self,((self, "stdinready"), x.stdin)), "selector") # \ #print (self.name,"OK, we're waiting....", len(self.inboxes["inbox"]), len(writeBuffer)) break # Break out of this loop except: # \ #print (self.name,"Unexpected error whilst trying to write to stdin:") print (sys.exc_info()[0] ) break # if count != len(d): # raise RuntimeError("Yay, we broke it") if stdout_ready: try: Y = os.read(x.stdout.fileno(),2048) if len(Y)>0: self.send(Y, "outbox") except OSError: e = sys.exc_info()[1] # print ("Mighty Bingle", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stdout_ready = 0 self.send(newReader(self,((self, "stdoutready"), x.stdout)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stdout:") print (sys.exc_info()[0]) pass if stderr_ready: # FIXME: This needs fixing before release try: Y = os.read(x.stderr.fileno(),2048) # TEMPORARY DIVERSION OF STDERR TO OUTBOX TOO # \ #if len(Y)>0: self.send(Y,"outbox") ## No particular plans for stderr except OSError: e = sys.exc_info()[1] # \ #print (self.name,"Mighty Jibble", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stderr_ready = 0 self.send(newReader(self,((self, "stderrready"), x.stderr)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stderr:") print (sys.exc_info()[0]) pass if self.dataReady("control"): shutdownMessage = self.recv("control") self.send(removeWriter(self,(x.stdin)), "selector") yield 1 x.stdin.close() yield 1 # \ #print (self.name,"UnixPipe finishing up") while self.dataReady("stdoutready"): # \ #print (self.name,"flushing") self.recv("stdoutready") try: Y = os.read(x.stdout.fileno(),10) while Y: self.send(Y, "outbox") Y = os.read(x.stdout.fileno(),10) # \ #print (self.name,"Mighty Floogly") except OSError: e = sys.exc_info()[1] continue except: break yield 1 # remove now closed file handles from the selector, so it doesn't stay # upset self.send(removeReader(self,(x.stderr)), "selector") self.send(removeReader(self,(x.stdout)), "selector") self.send(removeWriter(self,(x.stdin)), "selector") # \ #print (self.name,"sending shutdown") if not shutdownMessage: # \ #print (self.name,"new signal") self.send(Axon.Ipc.producerFinished(), "signal") # \ #print (self.name,"...sent") else: # \ #print (self.name,"old signal") self.send(shutdownMessage, "signal")
if not waiting: readsomething = False while len(self.outboxes["outbox"] ) < self.maxqueue and self.tryReadChunk(self.fd): readsomething = True pass if readsomething: self.selectorWait(self.fd) waiting = True if not self.done: self.pause() self.send(removeReader(self, self.fd), '_selectorask') os.close(self.fd) self.send(producerFinished(self), "signal") self.debug("IntelligentFileReader terminated") __kamaelia_components__ = (IntelligentFileReader, ) if __name__ == "__main__": class DebugOutput(component): def main(self): while 1: yield 1 self.pause()
def main(self): writeBuffer = [] shutdownMessage = False selectorService, selectorShutdownService, S = Selector.getSelectorServices( self.tracker) if S: S.activate() yield 1 yield 1 yield 1 self.link((self, "selector"), (selectorService)) # self.link((self, "selectorsignal"), (selectorShutdownService)) x = self.openSubprocess() self.send(newWriter(self, ((self, "stdinready"), x.stdin)), "selector") self.send(newReader(self, ((self, "stderrready"), x.stderr)), "selector") self.send(newReader(self, ((self, "stdoutready"), x.stdout)), "selector") # Assume all ready stdin_ready = 1 stdout_ready = 1 stderr_ready = 1 exit_status = x.poll() # while x.poll() is None success = 0 while exit_status is None: exit_status = x.poll() if (not self.anyReady()) and not (stdin_ready + stdout_ready + stderr_ready): # \ #print (self.name,"Mighty Foo", stdin_ready, stdout_ready, stderr_ready, len(self.inboxes["inbox"]), len(writeBuffer)) self.pause() yield 1 continue while self.dataReady("inbox"): d = self.recv("inbox") writeBuffer.append(d) if self.dataReady("stdinready"): self.recv("stdinready") stdin_ready = 1 if self.dataReady("stdoutready"): self.recv("stdoutready") stdout_ready = 1 if self.dataReady("stderrready"): self.recv("stderrready") stderr_ready = 1 if len(writeBuffer) > 10000: writeBuffer = writeBuffer[-10000:] if stdin_ready: while len(writeBuffer) > 0: d = writeBuffer[0] # d = writeBuffer.pop(0) try: count = os.write(x.stdin.fileno(), d) writeBuffer.pop(0) success += 1 except OSError: e = sys.exc_info()[1] success -= 1 # \ #print (self.name,"Mighty FooBar", len(self.inboxes["inbox"]), len(writeBuffer)) # Stdin wasn't ready. Let's send through a newWriter request # Want to wait stdin_ready = 0 writeBuffer = writeBuffer[len(writeBuffer) / 2:] self.send( newWriter(self, ((self, "stdinready"), x.stdin)), "selector") # \ #print (self.name,"OK, we're waiting....", len(self.inboxes["inbox"]), len(writeBuffer)) break # Break out of this loop except: # \ #print (self.name,"Unexpected error whilst trying to write to stdin:") print(sys.exc_info()[0]) break # if count != len(d): # raise RuntimeError("Yay, we broke it") if stdout_ready: try: Y = os.read(x.stdout.fileno(), 2048) if len(Y) > 0: self.send(Y, "outbox") except OSError: e = sys.exc_info()[1] # print ("Mighty Bingle", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stdout_ready = 0 self.send( newReader(self, ((self, "stdoutready"), x.stdout)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stdout:") print(sys.exc_info()[0]) pass if stderr_ready: # FIXME: This needs fixing before release try: Y = os.read(x.stderr.fileno(), 2048) # TEMPORARY DIVERSION OF STDERR TO OUTBOX TOO # \ #if len(Y)>0: self.send(Y,"outbox") ## No particular plans for stderr except OSError: e = sys.exc_info()[1] # \ #print (self.name,"Mighty Jibble", len(self.inboxes["inbox"]), len(writeBuffer)) # stdout wasn't ready. Let's send through a newReader request stderr_ready = 0 self.send( newReader(self, ((self, "stderrready"), x.stderr)), "selector") except: # \ #print (self.name,"Unexpected error whilst trying to read stderr:") print(sys.exc_info()[0]) pass if self.dataReady("control"): shutdownMessage = self.recv("control") self.send(removeWriter(self, (x.stdin)), "selector") yield 1 x.stdin.close() yield 1 # \ #print (self.name,"UnixPipe finishing up") while self.dataReady("stdoutready"): # \ #print (self.name,"flushing") self.recv("stdoutready") try: Y = os.read(x.stdout.fileno(), 10) while Y: self.send(Y, "outbox") Y = os.read(x.stdout.fileno(), 10) # \ #print (self.name,"Mighty Floogly") except OSError: e = sys.exc_info()[1] continue except: break yield 1 # remove now closed file handles from the selector, so it doesn't stay # upset self.send(removeReader(self, (x.stderr)), "selector") self.send(removeReader(self, (x.stdout)), "selector") self.send(removeWriter(self, (x.stdin)), "selector") # \ #print (self.name,"sending shutdown") if not shutdownMessage: # \ #print (self.name,"new signal") self.send(Axon.Ipc.producerFinished(), "signal") # \ #print (self.name,"...sent") else: # \ #print (self.name,"old signal") self.send(shutdownMessage, "signal")
try: Y = os.read(x.stdout.fileno(),10) while Y: self.send(Y, "outbox") Y = os.read(x.stdout.fileno(),10) # \ #print self.name,"Mighty Floogly" except OSError, e: continue except: break yield 1 # remove now closed file handles from the selector, so it doesn't stay # upset self.send(removeReader(self,(x.stderr)), "selector") self.send(removeReader(self,(x.stdout)), "selector") self.send(removeWriter(self,(x.stdin)), "selector") # \ #print self.name,"sending shutdown" if not shutdownMessage: # \ #print self.name,"new signal" self.send(Axon.Ipc.producerFinished(), "signal") # \ #print self.name,"...sent" else: # \ #print self.name,"old signal" self.send(shutdownMessage, "signal") # \
msg = self.recv("_selectorready") if not waiting: readsomething = False while len(self.outboxes["outbox"]) < self.maxqueue and self.tryReadChunk(self.fd): readsomething = True pass if readsomething: self.selectorWait(self.fd) waiting = True if not self.done: self.pause() self.send(removeReader(self, self.fd), '_selectorask') os.close(self.fd) self.send(producerFinished(self), "signal") self.debug("IntelligentFileReader terminated") __kamaelia_components__ = ( IntelligentFileReader, ) if __name__ == "__main__": class DebugOutput(component): def main(self): while 1: yield 1 self.pause() pipeline(