[Twisted-Python] How to create a new service and listen on a new port?

Eric Hsu nkeric at gmail.com
Fri May 27 04:17:01 EDT 2005


Now I've figured out the solution!
Just remove the "reactor.run()" as Jp suggestion!

Here is the sample code which fulfills my requirements:

class AClass(DatagramProtocol):
    def datagramReceived(self, data, addr):
        # do something...
        anotherService1 = reactor.listenUDP(port1, AnotherService())
        anotherService2 = reactor.listenUDP(port2, AnotherService())
        # ...
        anotherServiceN = reactor.listenUDP(portN, AnotherService())

        # if we want to stop one of the AnotherServiceS, just
        anotherServiceX.stopListening()
        
class AnotherService(DatagramProtocol):
     def datagramReceived(self, data, addr):
        # blah blah blah...

Thank you all guys!


2005/5/23, Eric Hsu <nkeric at gmail.com>:
> 2005/5/23, Jp Calderone <exarkun at divmod.com>:
> >   Since nothing is likely to call datagramReceived unless the reactor is running, and calling reactor.run() while the reactor is running is a terribly broken thing to do, the above code is likely mistaken in calling reactor.run().
> >
> >   I bet shutdown behavioor will become much nicer if you remove the call to reactor.run() here, and in any other similar places.
> >
> >   twistd will start and stop the reactor for you.
> >
> > Jp
>




More information about the Twisted-Python mailing list