[Twisted-Python] cleanup in twisted

Joachim Boomberschloss boomberschloss at yahoo.com
Sun May 22 09:46:46 MDT 2005


Hi!

I just jotted down a little mechanism for cleaning-up
in Twisted, and I wanted to see if:
1. other people think it's needed
2. other people manage to use it

What this does is enable one to define a __cleanup__
method which gets call either when the instance is
deleted, or when the reactor shuts down. It can return
a dereffed that delays the shutdown. The benefit is
being able to define communication-related stuff in
the cleanup method, which is not so useful to do in
__del__.

The current implementation uses pyDispatcher, but it
could easily be rewritten without it.

Feedback is welcomed!

Joe.

#----------------------------------------------------------------------------------------------------

from twisted.internet import reactor, defer, task

from dispatch import dispatcher, robustapply

#----------------------------------------------------------------------------------------------------

class CleanUpper:

	# our pydispatcher cleanup event
	ABOUT_TO_SHUTDOWN = "*about to shutdown*"

	def __init__(self):
		self._deferreds_to_wait_for_before_shutdown = []

		# register with twisted a special callback that
		# fires a pydispatcher event.
		reactor.addSystemEventTrigger('before', 'shutdown',
self._shutdown_handler)

	def _shutdown_handler(self):
		"""this simply dispatches a pydispatcher event"""
		results = dispatcher.send(
			signal = self.ABOUT_TO_SHUTDOWN,
			sender = self)
		# any deferred registered by cleanup handlers should
be waited for
		dl = defer.DeferredList([response for handler,
response in results if isinstance(response,
defer.Deferred)])
		return dl

	# PUBLIC METHODS
--------------------------------------------------------------------------

	def register(self, functionToRunBeforeShutdown):
		"""makes the given function get called prior to
communication infrastructure shutdown"""
		# register the given function with pydispatcher
		dispatcher.connect(
			functionToRunBeforeShutdown,
			signal = self.ABOUT_TO_SHUTDOWN,
			sender = self)

#----------------------------------------------------------------------------------------------------

# create an instance of CleanUpper
# and make its register method global
_c = CleanUpper()
register = _c.register
unregister = _c.unregister

#----------------------------------------------------------------------------------------------------

class Cleanuppable:
	""" Inherit from this if you want to do your cleanup
before
	    Twisted shuts down and not (only) when you get
deleted.
	    If you inherit from this do not implement
__del__, only
	    __cleanup__!
	"""

	def __init__(self):
		self._cleanuppable_state = "normal"		# normal,
deleted, or cleanupped
		self._cleanuppable_deferred = None
		# register cleanup method
	
register(self._cleanuppable_internal_cleanup_handler)

	def _cleanuppable_apply_cleanup(self):
		if hasattr(self, "__cleanup__"):
			# call user-defined cleanup method
			return robustapply.robustApply(self.__cleanup__)

	def _cleanuppable_internal_cleanup_handler(self):
		try:
			if self._cleanuppable_state == "normal":
				return self._cleanuppable_apply_cleanup()
			if self._cleanuppable_state == "deleted" and
self._cleanuppable_deferred is not None:
				return self._cleanuppable_deferred
		finally:
			self._cleanuppable_state = "cleanupped"

	def __del__(self):
		if self._cleanuppable_state == "normal":
			self._cleanuppable_state = "deleted"
			response = self._cleanuppable_apply_cleanup()
			if isinstance(response, defer.Deferred):
				self._cleanuppable_deferred = response

#----------------------------------------------------------------------------------------------------


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 




More information about the Twisted-Python mailing list