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

Jean-Paul Calderone exarkun at divmod.com
Fri Apr 14 17:32:28 EDT 2006

On Fri, 14 Apr 2006 17:15:25 -0200, Manlio Perillo <manlio_perillo at libero.it> wrote:
>I would like to have a function be called at the very end of the reactor
>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

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:
>Traceback (most recent call last):
>File "...\twisted\python\log.py",  line 53, in callWithLogger
>  	[...]
>--- <exception caught here> ---
>File "...\twisted\internet\selectreactor.py", line 139, in doReadOrWrite
>  why = getattr(selectable, method)()
>File "...\twisted\internet\udp.py", line 114, in doRead
>  data, addr = self.socket.recvfrom(self.maxPacketSize)
>  exceptions.AttributeError: 'Port' object has no attribute 'socket'
>Traceback (most recent call last):
>File "justmyscript.py", line 79, in ?
>  reactor.run()
>	[...]
>--- <exception caught here> ---
>File "...\twisted\python\log.py", line 53, in callWithLogger
>  return callWithContext({"system": lp}, func, *args, **kw)
>File "...\twisted\python\log.py", line 38, in callWithContext
>  return context.call({ILogContext: newCtx}, func, *args, **kw)
>File "...\twisted\python\context.py", line 59, in callWithContext
>  return self.currentContext().callWithContext(ctx, func, *args, **kw)
>File "...\twisted\python\context.py", line 37, in callWithContext
>  return func(*args,**kw)
>File "...\twisted\internet\selectreactor.py", line 149, in doReadOrWrite
>  self._disconnectSelectable(selectable, why, method=="doRead")
>File "...\twisted\internet\posixbase.py", line 256, in _disconnectSelectable
>  selectable.connectionLost(failure.Failure(why))
>File "...\twisted\internet\udp.py", line 207, in connectionLost
>  self.protocol.doStop()
>File "...\twisted\internet\protocol.py", line 490, in doStop
>  assert self.numPorts > 0
>  exceptions.AssertionError:
>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?


More information about the Twisted-Python mailing list