Created
December 31, 2010 23:29
-
-
Save stephenmcd/761424 to your computer and use it in GitHub Desktop.
Threaded function decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import thread | |
def nonblocking(func): | |
""" | |
Decorator that runs the given func in a separate thread | |
when called, eg:: | |
@nonblocking | |
def some_blocking_func(): | |
# Some long running code. | |
return | |
""" | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
thread.start_new_thread(func, args, kwargs) | |
return wrapper |
Thanks man. Probably doesn't scale out very well, but handy for small stuff.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Saves messing around setting up Celery for non-critical background messages and keeps things responsive.