1. concept from Io language
- Posted by PL Feb 02, 2009
- 1086 views
Hello, I discovered euphoria sometime ago and find it interesting (specially, due to speed, co-routines, ...)
Concerning co-routines, I found a nice concept in language Io.
Let's give a small example:
V1 := O1 M1
This line execute method "M1" of object O1 and put result in variable V1. Nothing special ...
V1 := O1 @M1
Do the same but, - create a "future" (a special object) V1 - create a co-routine to execute the method asynchronously - when method is ended, V1 become an variable and get result of method
During execution of method, - the program can continue - If you try to access V1 (ex: V2 := V1 ...), program block untill M1 is finished. - You can of course check if M1 is finished
@M1 is indeed, sugar for @(M1) where @ is a function that execute a function in a co-routine.
You can for example, do sth like:
V1 := URL with("http://example1.com/") @fetch V2 := URL with("http://example2.com/") @fetch V3 := URL with("http://example3.com/") @fetch . . . do sth with V1 , V2 , V3
They give a nice example: a minimal webserver where each request is executed in a co-routine:
WebRequest := Object clone do( handleSocket := method(aSocket, aSocket streamReadNextChunk request := aSocket readBuffer betweenSeq("GET ", " HTTP") f := File with(request) if(f exists, f streamTo(aSocket), aSocket streamWrite("not found")) aSocket close ) ) WebServer := Server clone do( setPort(8000) handleSocket := method(aSocket, WebRequest clone **@**handleSocket(aSocket) ) ) WebServer start
Could that be implemented in euphoria ?
2. Re: concept from Io language
- Posted by mattlewis (admin) Feb 02, 2009
- 1159 views
Hello, I discovered euphoria sometime ago and find it interesting (specially, due to speed, co-routines, ...)
Concerning co-routines, I found a nice concept in language Io.
From the Io Docs:
So it looks like at least superficially, we have this in the form of multi-tasking
Let's give a small example:
V1 := O1 M1
This line execute method "M1" of object O1 and put result in variable V1. Nothing special ...
V1 := O1 @M1
Do the same but, - create a "future" (a special object) V1 - create a co-routine to execute the method asynchronously - when method is ended, V1 become an variable and get result of method
During execution of method, - the program can continue - If you try to access V1 (ex: V2 := V1 ...), program block untill M1 is finished. - You can of course check if M1 is finished
Future plans for euphoria include the possibility of coroutines, generators and iterators, so this basic idea should happen at some point.
You can for example, do sth like:
V1 := URL with("http://example1.com/") @fetch V2 := URL with("http://example2.com/") @fetch V3 := URL with("http://example3.com/") @fetch . . . do sth with V1 , V2 , V3[snip]
Could that be implemented in euphoria ?
I think the above paradigm could probably be done with the current multitasking API, though I haven't worked with it enough to say how well it would suit what you're describing.
Matt
3. Re: concept from Io language
- Posted by PL Feb 03, 2009
- 1038 views
I've seen that euphoria uses co-routine... so functionality is here. What I found interesting in Io was the simplicity: just put @ in front of a procedure/function/method to execute it in a co-routine.
NB: by the way, any blocking IO call in Io, generate automatiquely a yield. So, you put yield in your code ONLY for CPU intensive activity.