Skip to content

Instantly share code, notes, and snippets.

@sshopov
Last active February 24, 2018 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sshopov/10099727 to your computer and use it in GitHub Desktop.
Save sshopov/10099727 to your computer and use it in GitHub Desktop.
A handy decorator to run a function in a new thread. It came from https://arcpy.wordpress.com/2013/10/25/using-os-startfile-and-webbrowser-open-in-arcgis-for-desktop/ Tags: arcpy python arcgis
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