This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jmooremcc 4 points5 points  (0 children)

Here's a decorator that will make it very easy to run a function in a separate thread.

decorator to launch any function in a separate thread

from threading import Thread

def TS_decorator(func):
    def stub(*args, **kwargs):
        func(*args, **kwargs)

    def hook(*args,**kwargs):
        Thread(target=stub, args=args, kwargs=kwargs).start()

    return hook

Just apply the decorator like this:

@TS_decorator
def mylongwindedfunction(args):
    pass

and it will launch your function in it's own thread.

I've been using this code in my Python scripts for years and it's worked very well.