1

I'm designing an API, that I would like to make asynchronous. On the other hand I would still like to keep a simple to use synchronous version of it. Is there a simple way to achieve this?

I imagine something like a decorator that auto generates blocking wrappers for my async def methods.

0

2 Answers 2

1

It's possible to make a decorator that just creates an event loop every time you want to run an async function, but that adds a lot of overhead and confusion.

Another common practice is to create a client that can accept a transport, that way you can just choose to make the transport sync or async, for example, this project https://github.com/aio-libs/aioelasticsearch, which takes the sync elasticsearch pkg, but adds an async transport that you can use with it.

Or elasticsearch's official solution to using async with their pkg https://elasticsearch-py.readthedocs.io/en/master/async.html

Anyways, if you ask me I think its better to stick to one thing, if your project benefits from async, just keep the client async.

Sign up to request clarification or add additional context in comments.

Comments

1

Running an async function requires an event loop that takes control over the execution.

Just consider every async function you want to convert as a separate program. What you are asking for is then running an async program.

# async version:
await asyncfunc(arg1, arg2, ...)

# sync version:
asyncio.run(asyncfunc(arg1, arg2, ...))

Writing such decorator should be is easy.

Of course, the overhead is big.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.