[Twisted-Python] reactor.addSystemEventTrigger("before", "shutdown", self.logout)

Manlio Perillo manlio_perillo at libero.it
Fri Apr 14 18:34:23 EDT 2006

Jean-Paul Calderone ha scritto:
> On Fri, 14 Apr 2006 17:15:25 -0200, Manlio Perillo
> <manlio_perillo at libero.it> wrote:
>> Hi.
>> I would like to have a function be called at the very end of the reactor
>> lifetime.
>> I have tried with the statement in the subject, where self.logout sends
>> a packet via UDP (self is an instance of a class derived from
>> DatagramProtocol).
> Services or the underlying APIs they rely on (addSystemEventTrigger) are
> the correct way to go about this.


>> But this seems to be not allowed.
>> I obtain:
> [...]
>> This means, in short, that in a shutdown event I can't use the reactor?
>> I think that this should be documented.
> You can, and many applications do.  Perhaps you have found a bug, or
> perhaps you are using system event triggers improperly. Could you post
> a minimal, complete example which will reproduce this behavior?

My system is Windows XP SP2, Python 2.4.3, Twisted 2.2.0

import sys

from twisted.python import log

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import defer, reactor

class Server(DatagramProtocol):
    def datagramReceived(self, data, addr):
        print "server: received from %s:%d" % addr, data
        self.transport.write("LOGGED OUT", addr)

class Client(DatagramProtocol):
    def __init__(self):
        self.deferred = defer.Deferred()

    def startProtocol(self):
        self.transport.connect("", 9000)

        reactor.addSystemEventTrigger("before", "shutdown", self.logout)

    def connectionRefused(self):
        print "unable to connect"

    def datagramReceived(self, data, addr):
        print "client: received from %s:%d" % addr, data

    def logout(self):

        return self.deferred

reactor.listenUDP(9000, Server())
reactor.listenUDP(0, Client())

reactor.callLater(2, reactor.stop)



The problem seems to be with the response.
There are no problems if logout does not return a deferred (but in this
case the client does not sees the LOGGED OUT response).

Is this an improper use of system event triggers?

Thanks and regards  Manlio Perillo

More information about the Twisted-Python mailing list