Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save labordus/593b7f26b3895e6b4d25 to your computer and use it in GitHub Desktop.
Save labordus/593b7f26b3895e6b4d25 to your computer and use it in GitHub Desktop.
import functools
import threading
# A decorator that will run its wrapped function in a new thread
def run_in_new_thread(function):
# functool.wraps will copy over the docstring and some other metadata
# from the original function
@functools.wraps(function)
def fn_(*args, **kwargs):
thread = threading.Thread(target=function, args=args, kwargs=kwargs)
thread.start()
thread.join()
return fn_
#usage:
import os, webbrowser
startfile = run_in_new_thread(os.startfile)
openbrowser = run_in_new_thread(webbrowser.open)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment