# -*- coding:utf-8 -*-

import threading

def threaded(**options):
    """
        make calling of func in a new thread.
        options:
            name -> default : decorated function name
            daemon -> default : False
            start -> default : True
        Example:
            @threaded(name='test_function', start=False, daemon=True)
            def test():
                print 'test'

            t = test()
            t.start()
            t.join()
    """
    def decorator(func):
        name = options.get('name', func.__name__)
        daemon = bool(options.get('daemon', False))
        start = bool(options.get('start', True))

        assert callable(func)

        def new_func(*args, **kwargs):

            t = threading.Thread(
                target=func, 
                name=name,
                args=args, 
                kwargs=kwargs
            )
            t.setDaemon(daemon)
            if start:
                t.start()
                t.setName('%s-%d' % (t.name, t.ident))
            return t

        return new_func

    return decorator


# -*- coding:utf-8 -*-

import threading

def threaded(**options):
    """
        make calling of func in a new thread.
        options:
            name -> default : decorated function name
            daemon -> default : False
            start -> default : True
        Example:
            @threaded(name='test_function', start=False, daemon=True)
            def test():
                print 'test'

            t = test()
            t.start()
            t.join()
    """
    def decorator(func):
        name = options.get('name', func.__name__)
        daemon = bool(options.get('daemon', False))
        start = bool(options.get('start', True))

        assert callable(func)

        def new_func(*args, **kwargs):

            t = threading.Thread(
                target=func, 
                name=name,
                args=args, 
                kwargs=kwargs
            )
            t.setDaemon(daemon)
            if start:
                t.start()
                t.setName('%s-%d' % (t.name, t.ident))
            return t

        return new_func

    return decorator


def make_thread(target, **options):
    return threaded(**options)(target)


def wait_threads(tasks, timeout=5):
    """
        output:
            bool -> if all tasks are finished.
    """
    deadline = time.time() + timeout
    while time.time() < deadline:
        if len(filter(lambda t: t.is_alive(), tasks)) == 0:
            return True

        time.sleep(0.2)

    return False


add wait_threads