Fabric post-run processing Python decorator
import traceback
from functools import wraps
from fabric.api import env
# global variable for add_hooks()
parent_task_name = ''
def add_post_run_hook(hook, *args, **kwargs):
'''Run hook after Fabric tasks have completed on all hosts
Example usage:
@add_post_run_hook(postrunfunc, 'arg1', 'arg2')
def mytask():
# ...
'''
def true_decorator(f):
return add_hooks(post=hook, post_args=args, post_kwargs=kwargs)(f)
return true_decorator
def add_hooks(pre=None, pre_args=(), pre_kwargs={},
post=None, post_args=(), post_kwargs={}):
'''
Function decorator to be used with Fabric tasks. Adds pre-run
and/or post-run hooks to a Fabric task. Uses env.all_hosts to
determine when to run the post hook. Uses the global variable,
parent_task_name, to check if ...Using a Python timeout decorator for uploading to S3
At work we are uploading many images to S3 using Python's boto library. However we are experiencing a RequestTimeTooSkewed error once every 100 uploads on average. We googled, but did not find a solution. Our system time was in sync and our file sizes were small (~50KB).
Since we couldn't find the root cause, we added a watchdog timer as a bandaid solution. We already use a retry decorator to retry uploads to S3 when we get a 500 Internal Server Error response. To this we added a timeout decorator which cancels the S3 upload if it takes ...
... read more »Two of the simplest Python decorator examples
After trying for about the fifth time, I think I am starting to understand Python decorators due largely to Jack Diederich's PyCon 2009 talk, Class Decorators: Radically Simple.
Jack's practical definition of a decorator is:
- A function that takes one argument
- Returns something useful
In many simple cases, a function decorator can be described more specifically:
- A function that takes one argument (the function being decorated)
- Returns the same function or a function with a similar signature
As Jack states in his talk, a decorator is merely syntactic sugar. The same functionality can be achieved without using the ...
... read more »Trying out a Retry decorator in Python
The Python wiki has a
Retry decorator example which retries calling a failure-prone function
using an
exponential backoff algorithm. I modified it slightly to check
for exceptions instead of a False return value to indicate
failure. Each time the decorated function throws an exception, the decorator
will wait a period of time and retry calling the function until the maximum
number of tries is used up. If the decorated function fails on the last try,
the exception will occur unhandled.
import time
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
"""Retry calling the decorated function using an ...About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(6)
-
aws
(10)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(14)
-
datetime
(4)
-
decorators
(4)
-
django
(40)
-
emacs
(22)
-
files_directories
(12)
-
git
(5)
-
hardware
(5)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(6)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(6)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(146)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(10)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(65)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(6)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan