[Twisted-Python] Asynchronicity
Hi, I have managed to get a simple server going. It seems to accept multiple connections correctly. I am a little puzzled as to how to achieve an asynchonous application using twisted. Twisted itself seems asynchronous, but my code is blocked by the call: reactor.run() Is there anyway I can return from the reactor to run other code, like GUI stuff, or other processing? My code: import GUI from zope.interface import Interface from twisted.internet import reactor from twisted.protocols import basic from twisted.internet.protocol import Protocol, Factory class CNetwork(basic.LineReceiver): def connectionMade(self): GUI.Print("New Client: %s" % self.transport.getPeer().host) self.send = lambda x: self.transport.write(x+"\r\n") ## if self.factory.server_data: ## self.send("\r\n".join(self.factory.server_data)) ## else: ## self.send("You are logged in") GUI.Print("Entering loop for client %s" % self.transport.getPeer().host) def connectionLost(self, reason): GUI.Print( "%s has closed the connection" % self.transport.getPeer().host ) def dataReceived(self, data): data = data.strip() if not data: self.transport.loseConnection() return GUI.Print("%s says %r" % (self.transport.getPeer().host,data)) self.transport.write("You said: %r\r\n" % data ) ## self.factory.server_data.append(data + "~~") GUI.Print("Ready to receive more data") class CNetworkFactory(Protocol): factory = Factory() factory.protocol = CNetwork def __init__(self, max_clients): self.max_clients = max_clients self.connected_clients = 0 self.server_data = [] def doStart(self): GUI.Print("Factory started -- server is waiting for a connection") def doStop(self): GUI.Print("Factory stopped -- server is now closed") def buildProtocol(self, addr): GUI.Print("Connection attempt....") return CNetwork() def main(): reactor.listenTCP(14500, CNetworkFactory(max_clients = 2)) reactor.run() if __name__ == "__main__": main() ##DO SOMETHING ELSE HERE! ################################### Thanks for your help _________________________________________________________________ The next generation of Hotmail is here! http://www.newhotmail.co.uk
On Sat, 09 Jun 2007 16:09:29 +0100, Simon Pickles <sipickles@hotmail.com> wrote:
Hi,
I have managed to get a simple server going. It seems to accept multiple connections correctly.
I am a little puzzled as to how to achieve an asynchonous application using twisted. Twisted itself seems asynchronous, but my code is blocked by the call:
reactor.run()
Quite so. reactor.run enters the Twisted mainloop and does not return until the mainloop is stopped (via reactor.stop).
Is there anyway I can return from the reactor to run other code, like GUI stuff, or other processing?
No, but you can call into your other code from Twisted event handlers or you can write a custom reactor which integrates an existing mainloop with Twisted's. As far as GUIs go, Twisted already includes reactors which integrate glib, gtk, gtk2, various kinds of Windows loops, wxPython, and a few others. Precisely how you integrate two mainloops depends on various details about the loops in question. Which GUI library are you trying to integrate with? Jean-Paul
participants (2)
-
Jean-Paul Calderone
-
Simon Pickles