I'm creating a programming language (a bytecode interpreter). It has enough features already that make it useful. But now I want to implement asynchronous programming as well. I have been researching about this topic alot on the internet but couldn't find anything useful.Even if I do find something it's about using asyncio not about implementing it.If there is something about implementation it is too language specific. My language supports functions and coroutines.
Coroutine Syntax:
function coro()
{
yield 1
yield 2
return 3 # finally return 3
}
var obj = coro()
println(obj.resume()) #1
println(obj.resume()) #2
println(obj.resume()) #3
println(obj.isAlive()) # false
The question is how would I go from coroutines to supporting asynchronous programming. I have heard of something called the event loop and I saw someone writing it in PyCon YouTube video. If I were to write his code's equivalent in my language that would be:
function coro()
{
yield 1
yield 2
return 3
}
function coro1()
{
println("here")
yield 10
println("here again")
return 20
}
#create an array of tasks(or queue)
var tasks = [coro(),coro(),coro1()]
while(len(tasks)!=0)
{
var curr = tasks[0]
#run the task
curr()
if(curr.isAlive()) #not finished
tasks.push(curr)
}
Is that it? Is that what asynchronous programming is all about?Creating coroutine objects,pushing them onto a queue and running them one by one? I know it's cooperative multitasking because coroutines are voluntarily suspending themselves.Also once a IO operation has started, how can a coroutine suspend itself in such a way that the IO operation continues running in the background?