Run in thread Python decorator

Here is a little neat decorator that will run a function in a thread:
import threading

def run_in_thread(fn):
    def run(*k, **kw):
        t = threading.Thread(target=fn, args=k, kwargs=kw)
        t.start()
    return run

It can be used for lazy updating analytics, cleanups or search index. An example:

class Search:

   @run_in_thread
   def add_item(self, ...):
       ...
Code · Python · Tips 15. Sep 2008
© Amir Salihefendic. Powered by Skeletonz.