So async cannot be called from regular def functions. I currently have a chain of regular function in which the last function in the chain sends http request to the database and once it receives the response, the response is sent back to the chain upto the first function which is the callee function that user calls. This chain does processing of requesting and receiving data.
ubernostrum[OP]1 points · Aug 2022 · yes, you can have a pony
If the last function becomes async, it's syntactically invalid to await it inside a non-async function.
But no matter what you'll need to set up some kind of event loop to run that function, so you could have the next-to-last function set it up and do it rather than trying to await it. Or you could async def and await all the way down the chain and at the top level put the first function into an async event loop.
If there's only one function running asynchronously in an event loop, I lose the power of async, am I not? That one async function will run until completion because there's no other coroutine to switch to inside an event loop. Am I thinking it correctly?
ubernostrum[OP]1 points · Aug 2022 · yes, you can have a pony
If you were hoping to get benefits out of going async, then yes, you generally want to go all-in on it. Having a mixed codebase is possible, but is more complex, requires more work to knit the two worlds together, and the overhead of it may eat up any performance gains you thought you'd get from async.
Your blog really helped me with my doubts on using async. I have one more question. I have one package that has a sync (blocking) method which handles I/O http request using the requests.Session class. The requirement is to replace request.Session with async httpx.Client class.
As you have already said that I can't await httpx Client inside a non-async method so if I just use asyncio.run(httpx.Client(...).get(...)), will that be sufficient to make this package's I/O asynchronous?
Note: This package is being used by an async service and because the service is using asynchronous I/O, requests.Session in this package is blocking the code and that's why the requirement is to replace it with async httpx Client.
So
asynccannot be called from regulardeffunctions. I currently have a chain of regular function in which the last function in the chain sends http request to the database and once it receives the response, the response is sent back to the chain upto the first function which is the callee function that user calls. This chain does processing of requesting and receiving data.If I make the last function asynchronous:
Does it mean that now I have to make all functions in the chain
asyncandawaitthe next callee function in the chain in order to make it work?ubernostrum [OP] 1 points · Aug 2022 · yes, you can have a pony
If the last function becomes async, it's syntactically invalid to
awaitit inside a non-async function.But no matter what you'll need to set up some kind of event loop to run that function, so you could have the next-to-last function set it up and do it rather than trying to
awaitit. Or you couldasync defandawaitall the way down the chain and at the top level put the first function into an async event loop.crawl_dht 1 points · Aug 2022
If there's only one function running asynchronously in an event loop, I lose the power of async, am I not? That one async function will run until completion because there's no other coroutine to switch to inside an event loop. Am I thinking it correctly?
ubernostrum [OP] 1 points · Aug 2022 · yes, you can have a pony
If you were hoping to get benefits out of going async, then yes, you generally want to go all-in on it. Having a mixed codebase is possible, but is more complex, requires more work to knit the two worlds together, and the overhead of it may eat up any performance gains you thought you'd get from async.
crawl_dht 1 points · Nov 2022
Your blog really helped me with my doubts on using async. I have one more question. I have one package that has a sync (blocking) method which handles I/O http request using the
requests.Sessionclass. The requirement is to replacerequest.Sessionwith asynchttpx.Clientclass.As you have already said that I can't await httpx Client inside a non-async method so if I just use
asyncio.run(httpx.Client(...).get(...)), will that be sufficient to make this package's I/O asynchronous?Note: This package is being used by an async service and because the service is using asynchronous I/O,
requests.Sessionin this package is blocking the code and that's why the requirement is to replace it with async httpx Client.