3 ms·
Why is it only "maybe async" and only "maybe await"? Isn't it clear at writing code time which one it is, async or sync?
by zelphirkalt 1y ago
Why is it only "maybe async" and only "maybe await"? Isn't it clear at writing code time which one it is, async or sync?
- PaulHoule 1y agoFor an http API (say something like boto3 or a client for arangodb) you might want to use the API from either a sync or async application. Since the code is almost all the same you can code generate a version of the API for both sync and async which is particularly easy if you use https://www.python-httpx.org/ https://www.python-httpx.org/ since you can use basically the same http client for both sides. One way to do it is write code like the sample I showed and use https://docs.python.org/3/library/ast.html https://docs.python.org/3/library/ast.html to scan the tree and either remove the maybe_await() or replace it with an await accordingly. You could either do this transformation when the application boots or have some code that builds both sync and async packages and packs the code up in PyPi. There are lots of ways to do it.
- zelphirkalt 1y agoWhy not just make a decision to query an API async and be done with it, instead of having something being "maybe"? Since this whole topic is about having a good way to have ones cake and also eat it, assuming one has such a way, I don't see any downside to choosing async for API calls.
- jcranmer 1y agoThere is some code that wants to be generic over its executor. This is particularly true of something like parsing code, where you logically have a "get more data" call and you really don't care about the details of the call otherwise. So if the user provided a synchronous "get more data" source, you'd want the parse method to be synchronous; if it were asynchronous, you'd want it the parse method to be asynchronous. That's basically the genesis of the idea of maybe-async. I've cooled tremendously on the idea, personally, because it turns out that a lot of code has rather different designs throughout the entire stack if you're relying on sync I/O versus async I/O, and this isn't all that useful in practice.