Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
Created December 31, 2010 23:29
Show Gist options
  • Save stephenmcd/761424 to your computer and use it in GitHub Desktop.
Save stephenmcd/761424 to your computer and use it in GitHub Desktop.
Threaded function decorator
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
@jbarham
Copy link

jbarham commented Apr 23, 2012

Nice! Saves messing around setting up Celery for non-critical background messages and keeps things responsive.

@stephenmcd
Copy link
Author

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