A decorator that takes a generator function and makes it thread-safe.
503735
def threadsafe_generator(generator_func):
    
    def decoration(*args, **keyword_args):
        """A thread-safe decoration for a generator function."""
        return ThreadSafeIter(generator_func(*args, **keyword_args))
    return decoration
527822
def synchronized(wrapped):
    """"""
    _lock = threading.RLock()

    @functools.wraps(wrapped)
    def _wrapper(*args, **kwargs):
        with _lock:
            return wrapped(*args, **kwargs)
    return _wrapper
1107827
def _synchronized(meth):
    """"""

    @functools.wraps(meth)
    def wrapper(self, *args, **kwargs):
        with self._lock:
            return meth(self, *args, **kwargs)

    return wrapper
106777
def decorator(caller, _func=None):
    """"""
    if _func is not None:  # return a decorated function
        # this is obsolete behavior; you should use decorate instead
        return decorate(_func, caller)
    # else return a decorator function
    if inspect.isclass(caller):
        name = caller.__name__.lower()
        doc = 'decorator(%s) converts functions/generators into ' \
              'factories of %s objects' % (caller.__name__, caller.__name__)
    elif inspect.isfunction(caller):
        if caller.__name__ == '<lambda>':
            name = '_lambda_'
        else:
            name = caller.__name__
        doc = caller.__doc__
    else:  # assume caller is an object with a __call__ method
        name = caller.__class__.__name__.lower()
        doc = caller.__call__.__doc__
    evaldict = dict(_call_=caller, _decorate_=decorate)
    return FunctionMaker.create(
        '%s(func)' % name, 'return _decorate_(func, _call_)',
        evaldict, doc=doc, module=caller.__module__,
        __wrapped__=caller)
1347338
def yieldefer(function):
    
    defer.inlineCallbacks
    return lambda *args, **kwargs: _yielddefer(function, *args, **kwargs)
299043
def started(generator_function):
    
    @wraps(generator_function)
    def wrapper(*args, **kwargs):
        g = generator_function(*args, **kwargs)
        next(g)
        return g
    return wrapper
821854
def decorator(cls, func_or_class):
        """"""
        resources = getattr(func_or_class, 'resources', [])
        resources.append(cls)
        func_or_class.resources = resources
        return func_or_class
833309
def spawn_thread(func, *args, **kwargs):
    """"""
    thread = threading.Thread(target=func, args=args, kwargs=kwargs)
    thread.daemon = True
    thread.start()
    return thread
452025
def wrap_sync(func):
    """"""
    @functools.wraps(func)
    def wrapped(*args, **kwargs):
        fut = asyncio.Future()
        def green():
            try:
                fut.set_result(func(*args, **kwargs))
            except BaseException as e:
                fut.set_exception(e)
        greenlet.greenlet(green).switch()
        return fut
    return wrapped
590061
def syscall_generator(generator):
    '''
    
    '''
    def _syscall(scheduler, processor):
        for e in generator():
            processor(e)
    return _syscall
368144
def optional_args_func(func) -> callable:
    
    if getattr(func, '_optional_args_func', False):
        return func

    is_generator = inspect.isgeneratorfunction(func)
    func_sig = inspect.signature(func)
    expects_nothing = not func_sig.parameters

    if is_generator:
        @functools.wraps(func)
        def wrapped(*args, **kwargs):
            if expects_nothing:
                yield from func()
            else:
                bound_arguments = func_sig.bind(*args, **{k: v for k, v in kwargs.items() if k in func_sig.parameters})
                yield from func(*bound_arguments.args, **bound_arguments.kwargs)
    else:
        @functools.wraps(func)
        def wrapped(*args, **kwargs):
            if expects_nothing:
                return func()
            else:
                bound_arguments = func_sig.bind(*args, **{k: v for k, v in kwargs.items() if k in func_sig.parameters})
                return func(*bound_arguments.args, **bound_arguments.kwargs)

    # Mark it so that we don't double wrap our own
    setattr(wrapped, '_optional_args_func', True)

    return wrapped
374649
def generator_checker_py3(gen, gen_type, bound_Generic, bound_typevars,
            bound_typevars_readonly, follow_fwd_refs, _recursion_check):
    
    initialized = False
    sn = None
    try:
        while True:
            a = gen.send(sn)
            if initialized or not a is None:
                if not gen_type.__args__[0] is Any and \
                        not _isinstance(a, gen_type.__args__[0], bound_Generic, bound_typevars,
                                bound_typevars_readonly, follow_fwd_refs,
                                _recursion_check):
                    tpa = deep_type(a)
                    msg = _make_generator_error_message(deep_type(a), gen, gen_type.__args__[0],
                            'has incompatible yield type')
                    _raise_typecheck_error(msg, True, a, tpa, gen_type.__args__[0])
# 					raise pytypes.ReturnTypeError(_make_generator_error_message(deep_type(a), gen,
# 							gen_type.__args__[0], 'has incompatible yield type'))
            initialized = True
            sn = yield a
            if not gen_type.__args__[1] is Any and \
                    not _isinstance(sn, gen_type.__args__[1], bound_Generic, bound_typevars,
                            bound_typevars_readonly, follow_fwd_refs, _recursion_check):
                tpsn = deep_type(sn)
                msg = _make_generator_error_message(tpsn, gen, gen_type.__args__[1],
                        'has incompatible send type')
                _raise_typecheck_error(msg, False, sn, tpsn, gen_type.__args__[1])
# 				raise pytypes.InputTypeError(_make_generator_error_message(deep_type(sn), gen,
# 						gen_type.__args__[1], 'has incompatible send type'))
    except StopIteration as st:
        # Python 3:
        # todo: Check if st.value is always defined (i.e. as None if not present)
        if not gen_type.__args__[2] is Any and \
                not _isinstance(st.value, gen_type.__args__[2], bound_Generic, bound_typevars,
                        bound_typevars_readonly, follow_fwd_refs, _recursion_check):
            tpst = deep_type(st.value)
            msg = _make_generator_error_message(tpst, gen, gen_type.__args__[2],
                    'has incompatible return type')
            _raise_typecheck_error(msg, True, st.value, tpst, gen_type.__args__[2])
# 			raise pytypes.ReturnTypeError(_make_generator_error_message(sttp, gen,
# 					gen_type.__args__[2], 'has incompatible return type'))
        raise st
282777
function (generatorFunc) {
            return function () {
                var generator = generatorFunc.apply(this, arguments);
                function doAction(action, arg) {
                    var result;    // in case error on first
                    // in case error on first
                    try {
                        result = generator[action](arg);
                    } catch (e) {
                        return new Reject(e);
                    }
                    if (result.done) {
                        return result.value;
                    }
                    return when(result.value, next, throwEx);
                }
                function next(v) {
                    return doAction('next', v);
                }
                function throwEx(e) {
                    return doAction('throw', e);
                }
                return next();
            };
        }
132598
def _spawn(func, *args, **kwargs):
    
    thr = Thread(target=func, args=args, kwargs=kwargs)
    thr.daemon = True
    thr.start()
    return thr
619579
function generatorToPromise(fn) {
    if (typeof fn !== 'function') {
        throw Error('fn is not a function');
    }
    if (!isGenerator(fn)) {
        // assume it's Promise-based
        return fn;
    }
    return co.wrap(fn);
}
464751
def maybe_a(generator):
    

    class MaybeAGenerator(ArbitraryInterface):
        """
        A closure class around the generator specified above, which generates
        either that generator or None.
        """

        @classmethod
        def arbitrary(cls):
            """
            Generate either the enclosed generator or None.
            """
            return arbitrary(one_of(None, generator))

    MaybeAGenerator.__name__ = ''.join(['maybe_a(', generator.__name__, ')'])
    return MaybeAGenerator
209898
def generator(self,gen,*args,**kwargs):
        
        with self(*args,**kwargs):
            for i in gen:
                yield i