How can async support dispatch between sync and async variants of the same code?

I moved this thread to Async-SIG.

On the main problem, I feel like I have to repeat myself – you’re better off inventing your own solution that works right for the framework.

One trick I’ve seen is a decorator that takes an async function, and adds a function attribute (e.g. named ‘sync’) that is a wrapper that calls the async version and waits for the result. E.g.

def add_sync_version(func):
  assert asyncio.iscoroutine(func)
  def wrapper(*args, **kwds):
    return asyncio.new_event_loop().run(func, *args, **kwds)
  func.sync = wrapper
  return func

I haven’t tested this version and there are dangers associated with creating a new event loop for this purpose, but you get the idea.