Posts

Showing posts from January, 2022

Coroutines In Python

Coroutines  are mostly used in cases of time-consuming programs, such as tasks related to machine learning or deep learning algorithms, or in cases where the program has to read a file containing a large number of data. In such situations, we do not want the program to read the file or data again and again, so we use coroutines to make the program more efficient and faster. Coroutines run endlessly in a program because they use a while loop with a true or 1 condition, so it may run until infinite time. Even after yielding the value to the caller, it still awaits further instruction or calls. We have to stop the execution of the coroutine by calling the coroutine.close() function. It is crucial to close a coroutine because its continuous running can take up memory space, as we have discussed in  Tutorial #75 , related to function caching. We can define a coroutine using the following statements. def myfunc ( ) : while True : value = ( yield ) Copy The while blo...