How to invoke `async` function from `cmd` (Python standard library, sync-async bridge, "coroutine ... was never awaited")?

One addition for future readers (pun intended :-)): If you get error

Future attached to a different loop

, it is because futures received from main event loop and the thread loop get mixed. In this case you need to use asyncio.run_coroutine_threadsafe():

    # this should be a reference to outer/main loop
    loop = asyncio.get_running_loop()    

    def do_myaction(self, arg):
        async def run():
            await asyncio.sleep(3)
            print("Some app results")

        asyncio.run_coroutine_threadsafe(run(),loop).result()

All in all my experience is that async/await quickly “creeps” up the entire application stack, which is not always desirable.

For my case I just needed to reuse common async functions from a server component. The client can be run synchronously in a blocking fashion. The easiest way here is to partially use asyncio.run(coro) in all code locations, which require bridging async to sync.

All functions should be completely aware, if run in context of an existing event loop or not. Everything else might be considered a code smell.