Skip to content

Instantly share code, notes, and snippets.

@mjpieters
Created April 27, 2012 07:34
Show Gist options
  • Save mjpieters/2506986 to your computer and use it in GitHub Desktop.
Save mjpieters/2506986 to your computer and use it in GitHub Desktop.
plone.app.async decorator template
_decoratedFunctions = {}
class _AsyncSomething(object):
"""Async job decorator
Tracks decorated function via a global mapping instead of storing
the function directly to remain pickleable.
"""
_name = ''
def __init__(self, func):
self._key = (func.__module__, func.__name__)
global _decoratedFunctions
_decoratedFunctions[self._key] = func
@property
def func(self):
global _decoratedFunctions
return _decoratedFunctions[self._key]
def __repr__(self):
return '%s.%s' % (self._key)
def __call__(self, *args, **kw):
# Do pre-call stuff here
self.func(context, obj, *args, **kw)
# Do post-call stuff here
def asyncSomething(func):
"""Decorator to enable multi-step batching"""
return _AsyncSomething(func)
# Example use
@asyncSomething
def someFunctionForAsyncUse(context, obj):
# Blah blah
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment