def run_coroutine_threadsafe(coro: Union[(Coroutine, Generator)], loop: AbstractEventLoop) -> concurrent.futures.Future:
    'Submit a coroutine object to a given event loop.\n\n    Return a concurrent.futures.Future to access the result.\n    '
    ident = loop.__dict__.get('_thread_ident')
    if ((ident is not None) and (ident == threading.get_ident())):
        raise RuntimeError('Cannot be called from within the event loop')
    if (not coroutines.iscoroutine(coro)):
        raise TypeError('A coroutine object is required')
    future = concurrent.futures.Future()

    def callback() -> None:
        'Handle the call to the coroutine.'
        try:
            _chain_future(ensure_future(coro, loop=loop), future)
        except Exception as exc:
            if future.set_running_or_notify_cancel():
                future.set_exception(exc)
            else:
                _LOGGER.warning('Exception on lost future: ', exc_info=True)
    loop.call_soon_threadsafe(callback)
    return future
Submit a coroutine object to a given event loop.

Return a concurrent.futures.Future to access the result.
def run_coroutine_threadsafe(coro: Union[(Coroutine, Generator)], loop: AbstractEventLoop) -> concurrent.futures.Future:
    'Submit a coroutine object to a given event loop.\n\n    Return a concurrent.futures.Future to access the result.\n    '
    ident = loop.__dict__.get('_thread_ident')
    if ((ident is not None) and (ident == threading.get_ident())):
        raise RuntimeError('Cannot be called from within the event loop')
    if (not coroutines.iscoroutine(coro)):
        raise TypeError('A coroutine object is required')
    future = concurrent.futures.Future()

    def callback() -> None:
        'Handle the call to the coroutine.'
        try:
            _chain_future(ensure_future(coro, loop=loop), future)
        except Exception as exc:
            if future.set_running_or_notify_cancel():
                future.set_exception(exc)
            else:
                _LOGGER.warning('Exception on lost future: ', exc_info=True)
    loop.call_soon_threadsafe(callback)
    return future