Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
contego / home / tvault / .virtenv / lib / python2.7 / site-packages / contego / async_utils.py
Size: Mime:
# Copyright 2015 TrilioData Inc.
# All Rights Reserved.

from threading import Thread, Event, BoundedSemaphore
from functools import wraps
from contego.exception import RetryException


def run_async(func):
    """
        run_async(func)
            function decorator, intended to make "func" run in a separate
            thread (asynchronously).
            Returns the created Thread object

            E.g.:
            @run_async
            def task1():
                do_something

            @run_async
            def task2():
                do_something_too

            t1 = task1()
            t2 = task2()
            ...
            t1.join()
            t2.join()
    """
    @wraps(func)
    def async_func(*args, **kwargs):
        func_hl = Thread(target=func, args=args, kwargs=kwargs)
        func_hl.start()
        return func_hl

    return async_func


def run_async_by_throttle(throttling):
    def run_async(func):
        @wraps(func)
        def async_func(*args, **kwargs):
            event = Event()

            def thread_wrapper_func(*args, **kwargs):
                thevent = kwargs.pop('thread_start_event')
                thfunc = kwargs.pop('actual_func')
                thevent.set()
                try:
                    thfunc(*args, **kwargs)
                finally:
                    throttling.release()

            if throttling.acquire(False):
                try:
                    kwargs.update(
                        {'thread_start_event': event, 'actual_func': func})
                    func_hl = Thread(
                        target=thread_wrapper_func, args=args, kwargs=kwargs)
                    func_hl.start()
                    event.wait()
                    return func_hl
                except BaseException:
                    throttling.release()
                    raise RetryException("Retry....")
            else:
                raise RetryException("Retry....")

        return async_func
    return run_async