[Grok-dev] Execute function when server is shut down

Hector Blanco white.lists at gmail.com
Thu Jan 13 10:42:38 EST 2011


Mmm...  I couldn't get it working right with atexit...

1st try:
====== app.py =====
#Put in in the __main__ and register
def onClose():
	log.debug("::onClose > Executing!")
atexit.register(onClose)

#Begin the server class itself
class Server(grok.Application, grok.Container):
	def __init__(self):
		try:
			super(Server, self).__init__()
		# Rest of code
==============

2nd try:
====== app.py =====
#Put in in the 'server' class
class Server(grok.Application, grok.Container):
	def onClose(self):
		log.debug("::onClose > Executing!")

	def __init__(self):
		try:
			super(Server, self).__init__()
			atexit.register(getattr(self, "onClose"))

		# Rest of code
==============

I don't think there's a need to say, but registering it with
decorators doesn't work either.

I press ^C to stop the server... maybe atexit is not intended to
handle SIGINTs signals?

... will keep trying and keep you posted ...

Thanks for the hint, though... I didn't know that atexit thingy and
can turn out to be very useful in other areas...



2011/1/13 Jan-Wijbrand Kolman <janwijbrand at gmail.com>:
> On 1/13/11 1:09 AM, Leonardo Rochael Almeida wrote:
>> Maybe what you want to do is register atexit methods:
>>
>> http://docs.python.org/library/atexit.html
>>
>> On Wed, Jan 12, 2011 at 18:30, Hector Blanco<white.lists at gmail.com>  wrote:
>>> Yeah... I should've thought about that before sending the mail...
>>> Since the __init__ is only called when I create a new instance of my
>>> server, I should have realized that, in the best case the __del__
>>> method would be called when an instance of the server is removed in
>>> the server control page.
>>>
>>> Surprisingly (well... surprisingly for a newbie that doesn't have much
>>> idea about how all that works) that doesn't happen... The __del__
>>> method is not called when I remove an application.
>>>
>>> Well... maybe a workaround might be catching the signal send by the
>>> operative system... (just saying... I don't know if that makes much
>>> sense)
>
> I'm not sure though of you have enough database context at this point in
> the life cycle of the app to do whatever you'd like to do.
>
> In any case, please report what you find, others might benefit from this
> as well! (and I'd still like to find out exactly how a Zope app is
> shutdown).
>
> regards, jw
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> https://mail.zope.org/mailman/listinfo/grok-dev
>


More information about the Grok-dev mailing list