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

mark mark at junklight.com
Mon May 23 04:28:29 EDT 2005


Killing the process is quite normal in early stage development.  
Twisted even helps with this by writing a pid file if you demonize it.

There are a number of more elegant techniques. If you intend to  
always run it from the command line as a non demon then write a  
control-c handler that gracefully stops your process.

def ctrlCHandler(*whatever):
     print "Interupted"
     for thread in threads:
         thread.stop()
         thread.join()

signal.signal(signal.SIGINT, ctrlCHandler)

(this is from a non-twisted application with threads :-) - I don't  
know if the signal thingy works on windows either)

The other option is to build a stop mechanism - I'm not sure from  
your description what your app does but you could send it a 'STOP'  
datagram for example that would cause it to stop nicely. Or when you  
have your client connection set up allow the client to terminate the  
process in a similar way.

you should be able to search the list at http://gmane.org/ although I  
find gmane has a very poor UI and I find its search a bit mysterious  
too.

cheers

mark



On 23 May 2005, at 06:49, Eric Hsu wrote:

> Hi all,
>
> This is my first post here, and I'm not a native English speaker, so
> please bear with me :)
> I'm trying to implement something like this:
>
>  * A UDP server listening on port1 for incoming request
>  * A client sends the requset to port1
>  * The server confirms the request and creates another (different) UDP
> service on a new port (port2)
>  * The client then would be able to connect to port2 and do some  
> other things...
>
> By reading this "Writing a New Plug-In for mktap"
>  - http://twistedmatrix.com/projects/core/documentation/howto/ 
> plugin.html
>
> I've implemented a UDP server and could run it with "twisted -f".
> Now the server receives request on port1, and creates a new service at
> port2 for client to connect to it.
>
> I implemented this as the following:
>
> class FooServer(DatagramProtocol):
>     def datagramReceived(self, data, addr):
>         # do something to verify the request and send the client with
> a confirm msg
>         reactor.listenUDP(port2, AnotherService())
>         reactor.run()
>
> class AnotherService(DatagramProtocol):
>      # ...
>
> I got this up and run under windows cmd console, however, whenever I
> try to stop the server with Ctrl + c or close the cmd console, the
> server died in a dead loop which keeps produces tone of error
> messages. The only way to quit that console is to "kill" it...
>
> I know there must be something wrong. I would like to know:
>   * How could I accomplish this in a more elegant/"formal" way?
>
> I'm very new to twisted and this mailing list, if someone has asked
> similar question before, please tell me how to find it. (BTW, how
> could I search the mailinglist?)
>
> Thanks in advance! :D
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>





More information about the Twisted-Python mailing list