What does it really mean ? when code gets suspended who gains the control ?

Frankly speaking when i heard of this term of suspending functions , it didn’t really resonate with me. I had questions such as

1. What does it really mean for a function to be a suspending function ?

2. If it gets suspended , who gets the control ?

3. how does a suspended function gets resumed ? Who resumes it ?

I came across this interesting post on stack overflow https://stackoverflow.com/questions/44214004/kotlin-alternative-to-pythons-coroutine-yield-and-send

This code shows the usage of generator as shown. While the next snippet shows the implementation of ES6-style generator.

As the comment tells this is an implementation of ES6-style generator. Basically ES6-style generator not only produces values but also allows other code interacting with it to pass the values to generator as well. Just the way as shown in the example above.

While there is comment that …

This code works just as well as its Python version, however it is not idiomatic for multitude of reasons ….(Read through stack overflow comment) I find thats this code really helps us understand what a suspending function is ?

So lets turn our attention to what a suspending function is ?

When you load the above code in Intellij , you will find that the yield() is a suspending function. The arrow with ~ , in the left column points to the places in code where the code will be suspended because of a call to suspending function.

So lets try to address What is suspending function ?

A suspending function is a function which instead of running to completion voluntarily releases control back to caller.

Lets focus on main() function now and see how this is true.

line 27 , really calls kotlincoro() function but instead of running to completion, what gets returned is a generator. Nothing interesting happens apart from this. Not a single line in the function kotlincoro() gets executed yet. Whaaaaaaaaat ? yes thats true.

When g.next(“”) gets called on line 31, thats when the kotlincoro() function gets executed and with the call to yield(“hello”) it suspends and returns control back to main() function and this string “hello” gets assigned to variable val initdata

Now since the control is in main function , we might wonder when is the rest of the code of the kotlincoro() gets executed ? Ie When does kotlincoro() resumes its execution [ Remember its suspended at yield(“Hello”) ]

The answer is it depends on the main() function. So nothing gets executed in kotlincoro() unless main() function calls g.next(“whats up) on line 35

Now the control goes back to kotlincoro() and the line 9 in the snippet collects the string “whats up” in variable val coroData

Now the control is in kotlincoro() , when does the rest of the main() function gets executed ? it really depends if the kotlincoro() suspends again or not ?

Now in our case , kotlincoro() runs till line 16 and suspends on yield(“Bye”)

Main function receives this value in variable val data on line 35 and runs till line no 39 where we send another value “Howdy there ?” to kotlincoro() which is suspended on line 16 , which upon receiving the value , runs till completion and control again returns back to main.

So to recap answers to our questions

What does it really mean for a function to be a suspending function ?The function is said to be a suspending function, if instead of running to completion it relinquishes control back to driver.

2. If it gets suspended , who gets the control ?

When a suspending function gets suspended, the driver gets control. The reason i am using the term driver instead of caller , is we are driving kotlincoro() function through many suspension points using g.next()

Remember when g = kotlincoro() gets executed , a generator gets created and nothing interesting happens , till we drive the generator using g.next()

3.how does a suspended function gets resumed ? Who resumes it ?

Again, as mentioned in the above answer, its the driver who resumes the execution of suspended function from previous suspension point.

Now if you have stuck with me this far, I really hope that , lets look at the output of the program

If you give a careful thought , it might be important to notice that coroutines with the suspending functions need to be run under the guidance of driver. Otherwise its not really possible for them to proceed. Thats where event loops come into the picture.

I hope that , this has given you something to think more about coroutines.

With that i would like to thank you for reading this post.

In the next post, I will try to demonstrate , how to create an event loop and run multiple coroutines and sort of achieve Round-Robin schedular using generators and event loop.

You can also check out my other articles as well and see if you like them as well.