What are async functions? | Sololearn: Learn to code for FREE!

+2

What are async functions?

I have searched it on google. But I didnt understood much. Posting here cause I need "simple words" explaination.

11/26/2020 7:17:22 PM

HET SHAH

8 Answers

New Answer

+6

[Part 1 of 2] HET SHAH Expanding further on Steven M's explanation, let's consider a function that uploads a file to a remote server. upload(filepath) Let's say that file is rather large and might take a minute to upload. If your program does nothing else, then it might not matter. The function is called and will complete in 1 minute. Now consider the following code with a list of 5 large files: for file in filepaths: upload(file) This is equivalent to: upload(fileList[0]) upload(fileList[1]) upload(fileList[2]) upload(fileList[3]) upload(fileList[4]) Here, the 2nd function is called after the 1st function completes. The 3rd function starts after the 2nd completes... and so on and so forth. If each call takes 1 minute, then it will take 5 minutes to complete all 5 uploads. This is known a serial (or synchronous) processing.

+5

"Fire and forget"... async doesn't wait for confirmation of request delivery. Sync waits for a response and it does not perform another operation until a proper response is given; this makes it a blocking call. https://medium.com/work-insight/fire-and-forget-c611053245f8

+5

[Part 2 of 2] With async, the 1st function is called and immediately exits before the upload is completed. The upload will just occur in the background without blocking the execution of the next code. Then, the 2nd function will be called and do the same where it exits before its upload is completed. All 5 functions might be called before the first upload is completed. This is what is meant by "fire and forget". The function fired (or was called) and then "forgotten" as in... it doesn't wait for completion to move on. This is also known as parallel or concurrent or asynchronous processing. Essentially, all 5 uploads are occurring simultaneously allowing for the process to complete quicker than one-by-one serial processing. Here's a more indepth article that might help you understand this further: https://realpython.com/async-io-python/

+3

Async means that it must be repeated or looped as long as It means that it will never stop running

+1

πŸ₯‡πŸ‘©β€πŸ’» Kintu Michael Evans πŸ”₯πŸ”₯ Can you go a little deeper?

+1

David Carroll Thanks a lot! Gotcha!πŸ‘πŸ˜„

+1

Steven M πŸ₯‡πŸ‘©β€πŸ’» Kintu Michael Evans πŸ”₯πŸ”₯ Thank you for your assistance!πŸ™Œ

0

Async functions its functions which parallel executed