(loop, func, *args, **kwargs)
Run coroutine in loop running in separate thread.
Run coroutine in loop running in separate thread.
Run coroutine in loop running in separate thread .
def sync(loop, func, *args, **kwargs):
    """
    Run coroutine in loop running in separate thread.
    """
    # This was taken from distrbuted/utils.py

    timeout = kwargs.pop('callback_timeout', None)

    e = threading.Event()
    main_tid = get_thread_identity()
    result = [None]
    error = [False]

    @gen.coroutine
    def f():
        try:
            if main_tid == get_thread_identity():
                raise RuntimeError("sync() called from thread of running loop")
            yield gen.moment
            thread_state.asynchronous = True
            future = func(*args, **kwargs)
            if timeout is not None:
                future = gen.with_timeout(timedelta(seconds=timeout), future)
            result[0] = yield future
        except Exception:
            error[0] = sys.exc_info()
        finally:
            thread_state.asynchronous = False
            e.set()

    loop.add_callback(f)
    if timeout is not None:
        if not e.wait(timeout):
            raise gen.TimeoutError("timed out after %s s." % (timeout,))
    else:
        while not e.is_set():
            e.wait(10)
    if error[0]:
        six.reraise(*error[0])
    else:
        return result[0]
def sync ( loop , func , * args , * * kwargs ) : # This was taken from distrbuted/utils.py timeout = kwargs . pop ( 'callback_timeout' , None ) e = threading . Event ( ) main_tid = get_thread_identity ( ) result = [ None ] error = [ False ] @ gen . coroutine def f ( ) : try : if main_tid == get_thread_identity ( ) : raise RuntimeError ( "sync() called from thread of running loop" ) yield gen . moment thread_state . asynchronous = True future = func ( * args , * * kwargs ) if timeout is not None : future = gen . with_timeout ( timedelta ( seconds = timeout ) , future ) result [ 0 ] = yield future except Exception : error [ 0 ] = sys . exc_info ( ) finally : thread_state . asynchronous = False e . set ( ) loop . add_callback ( f ) if timeout is not None : if not e . wait ( timeout ) : raise gen . TimeoutError ( "timed out after %s s." % ( timeout , ) ) else : while not e . is_set ( ) : e . wait ( 10 ) if error [ 0 ] : six . reraise ( * error [ 0 ] ) else : return result [ 0 ]