def call_sync(func):
    """Decorates a function to be called sync on the loop thread"""

    @wraps(func)
    def wrapper(self, *args, **kw):
        """Wraps instance method to be called on loop thread"""

        # Just return when already on the event thread
        if self.thread.ident == get_ident():
            return func(self, *args, **kw)

        barrier = Barrier(2)
        result = None
        ex = None

        def call():
            """Calls function on loop thread"""
            nonlocal result, ex
            try:
                result = func(self, *args, **kw)
            except Exception as exc:
                ex = exc
            finally:
                barrier.wait()

        self.loop.call_soon_threadsafe(call)
        barrier.wait()
        if ex:
            raise ex or Exception("Unknown error")
        return result

    return wrapper
def call_sync ( func ) : @ wraps ( func ) def wrapper ( self , * args , ** kw ) : if self . thread . ident == get_ident ( ) : return func ( self , * args , ** kw ) barrier = Barrier ( 2 ) result = None ex = None def call ( ) : nonlocal result , ex try : result = func ( self , * args , ** kw ) except Exception as exc : ex = exc finally : barrier . wait ( ) self . loop . call_soon_threadsafe ( call ) barrier . wait ( ) if ex : raise ex or Exception ( "Unknown error" ) return result return wrapper
Decorates a function to be called sync on the loop thread