Re: [Twisted-Python] UDP asynchronous communication
Trust me I've read the document and after: reactor.listenUDP(...) reactor.run() the *client* just waits for incoming traffic. I know that Twisted is not supposed to be like that. But please take a moment and read this: http://twisted.sourceforge.net/TwistedDocs-1.1.0/howto/udp.html#auto3 before you give me a *prebuilt* answer. Thank you. Tommi Virtanen wrote:
Adrian Libotean wrote:
Now the problem I faced is that from the examples I found on the web (http://twisted.sourceforge.net/TwistedDocs-1.1.0/howto/udp.html), for multicast UDP messages, are done using listenUDP on both sides (server and client) and this approach is not good for me because I need a non-blocking communication mechanism on the client-side.
Sounds _very_ much like you have misunderstood something. Twisted functionality is almost without exception non-blocking. Please re-read and try to ask a more specific question.
Adrian Libotean wrote:
Trust me I've read the document and after:
reactor.listenUDP(...) reactor.run()
the *client* just waits for incoming traffic.
That just tells me you have not understood twisted yet. Set up things to happen, then call reactor.run() at the end of your main function. If you only set up one thing to happen, only one thing will happen. That may be alright for the example, which only wanted to do one things; real applications tend to be a bit more complex. Also, please do not top-post.
Tommi Virtanen wrote:
Adrian Libotean wrote:
Trust me I've read the document and after:
reactor.listenUDP(...) reactor.run()
the *client* just waits for incoming traffic.
Set up things to happen, then call reactor.run() at the end of your main function.
That's the problem: I just want to start the client and then, when the user selects "Scan" from the interface, I must send the broadcast and then act on replies from servers completely independent of the main thread of the application. So I have two choices: I run the reactor in a separate thread, or I find a way to do a non-blocking communication using onReceive/onSend events.
Also, please do not top-post.
Sorry for that.
So I have two choices: I run the reactor in a separate thread, or I find a way to do a non-blocking communication using onReceive/onSend events.
no you don't understand the bigger picture with Twisted, it is a hollywood system, "Don't call us, we'll call you". If you would stop for a minute, take a step back and go READ about ZeroConf/Apple Rendezvous and then look at the mDNS implementations in Python you might understand you are doing things backwards. You register callbacks with Twisted, it calls them when it needs to you. Read about the Application and Service objects, what you want to do is more than is in the tutorial and examples. My suggestion is to DO the entire Echo how-to over and over until you "get it" about the Application and Service interfaces and objects and the .tac file concept.
So I have two choices: I run the reactor in a separate thread, or I find a way to do a non-blocking communication using onReceive/onSend events.
no you don't understand the bigger picture with Twisted, it is a hollywood system, "Don't call us, we'll call you". If you would stop for a minute, take a step back and go READ about ZeroConf/Apple Rendezvous and then look at the mDNS implementations in Python you might understand you are doing things backwards. You register callbacks with Twisted, it calls them when it needs to you. Read about the Application and Service objects, what you want to do is more than is in the tutorial and examples. My suggestion is to DO the entire Echo how-to over and over until you "get it" about the Application and Service interfaces and objects and the .tac file concept.
On Sun, 10 Apr 2005 19:18:38 +0300, Adrian Libotean <adrian.libotean@asylum-studios.ro> wrote:
Tommi Virtanen wrote:
Adrian Libotean wrote:
Trust me I've read the document and after:
reactor.listenUDP(...) reactor.run()
the *client* just waits for incoming traffic.
Set up things to happen, then call reactor.run() at the end of your main function.
That's the problem: I just want to start the client and then, when the user selects "Scan" from the interface, I must send the broadcast and then act on replies from servers completely independent of the main thread of the application.
So I have two choices: I run the reactor in a separate thread, or I find a way to do a non-blocking communication using onReceive/onSend events.
The latter sounds like a great option to me. Using a fictional widget library and a mega trivial protocol that just sends around "int:string" packets, here's how you do it: from twisted.internet import protocol, defer class ServiceDiscoveryDatagramProtocol(protocol.DatagramProtocol): def __init__(self): self.packets = {} self.counter = 0 def _makeAPacket(self, hint): self.counter += 1 d = self.packets[self.counter] = defer.Deferred() return d, '%d:%s' % (self.counter, hint) def _parseAPacket(self, bytes): counter, payload = bytes.split(':') return self.packets.pop(int(counter)), payload def discoverSomething(self, hint): d, pkt = self._makeAPacket(hint) self.transport.write(pkt) return d def datagramReceived(self, dgram, addr): d, stuff = self._parseAPacket(dgram) d.callback(stuff) from fiction import widgetlib class MainWidget(widgetlib.Frame): def __init__(self, proto): self.proto = proto self.discoverButton = widgetlib.Button(self, onClick=self._clicked) self.textArea = widgetlib.Text(self, 'Nothing to see') self.textArea.show() self.discoverButton.show() def _clicked(self): d = self.proto.discoverSomething('puppies') d.addCallback(self._discovered) def _discovered(self, what): self.textArea.setText(str(what)) proto = ServiceDiscoveryDatagramProtocol() reactor.listenUDP(0, proto) main = MainWidget(proto) main.show() reactor.run() As you can see, all that is involved is hooking up sources of events to code which is interested in events. Your widget library should generate events for user input. Twisted will generate events for network traffic. Each lets you define a function which is invoked when events occur. All you need to do is define the functions in such a way as to make your program do what you want it to do :) Hope this helps, Jp
participants (4)
-
Adrian Libotean
-
jarrod roberson
-
Jp Calderone
-
Tommi Virtanen