CodyKochmann
2/5/2017 - 12:48 PM

A simple decorator for functions that you wanna throw in the background outside of the main thread.

A simple decorator for functions that you wanna throw in the background outside of the main thread.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: codykochmann
# @Date:   2017-02-04 08:10:05
# @Last Modified 2017-02-05
# @Last Modified time: 2017-02-05 07:46:49

""" A simple decorator for functions that you wanna throw in the
    background outside of the main thread
"""

from threading import Timer
import wrapt

@wrapt.decorator
def background(wrapped, instance, args, kwargs):
    """ decorator that makes the function run in its own thread
        in order to collect the results of each thread, just have
        it append its output to a global list or dict
    """
    # 0.000001 is how many seconds to wait until it starts the new thread
    new_thread = Timer(0.000001, wrapped, args, kwargs)
    new_thread.start()
    return