Last active
February 24, 2018 21:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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