Background: I'm working on a discord bot that uses requests. The requests are async, so I'm using the library asgiref.sync
(I know I obviously can't use this function for async functions.)
I implemented sync_to_async into all the requests and things that may take long to process. The function works. However, I'm not sure if this will cause underlying issues in the future. If it does, is there an alternative?
CodePudding user response:
Async does not magically make your program runs in parallel. Within an async function there needs to be certain points where the function "gives up control", like when waiting for a response from a remote server.
If the function you 'converted' to async either (1) CPU-bound or (2) not giving up control by invoking an await
statement somewhere, or (3) both, then the function will run until completion until it finishes.
To put it in another way: async is cooperative multitasking. Async functions must, at certain points "hands over" control to the async loop to enable others to run.