3 ms·
Oh you can do it with identical signatures. Let’s say we are making http requests, with the sync code using requests and the async code using aiohttp. Your pu
by spamizbad 8y ago
Oh you can do it with identical signatures.
Let’s say we are making http requests, with the sync code using requests and the async code using aiohttp.
Your public methods all call a private _request method with a verb, a url, and optionally something to be seriaized to json.
Your async _request method does need to do a small bit of code around sessions: make one first if one hasn’t been made for the class instance yet (or override your __init__ and take an existing one as an arg) but at the end of the day it’s just returning a coroutine that’ll ultimately give you a deserialized response. Sync can just be more direct and return a dict. Both have the same signature.
- Xophmeister 8y agoRight, so you can just return a coroutine, so the return type of your method is something like `Union[dict, Coroutine]`. However, then downstream (ultimately) has to handle the dichotomy, where the `dict` is as is, but the coroutine needs to be `await`ed. You can do it, but I personally think it's messy. It's what I really dislike about Python's async model, which you almost necessarily encounter because the stdlib is mostly not async.