Forked from sshopov/run_in_new_thread_decorator.py
Last active
August 29, 2015 14:10
-
-
Save labordus/593b7f26b3895e6b4d25 to your computer and use it in GitHub Desktop.
This file contains hidden or 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