Created
July 11, 2014 14:35
-
-
Save scoulondre/9bfd3dee7ee236beaaa1 to your computer and use it in GitHub Desktop.
Go syntax in Python Goless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Using Goless: https://github.com/rgalanakis/goless | |
########################################################## | |
from goless import chan, select, rcase, go | |
import time | |
c1 = chan() | |
c2 = chan() | |
go(lambda: time.sleep(1) or c1.send('one')) | |
go(lambda: time.sleep(2) or c1.send('two')) | |
for i in range(2): | |
case, val = select([rcase(c1), rcase(c2)]) | |
print(val) | |
########################################################## | |
Equivalent to https://gobyexample.com/select | |
package main | |
import "time" | |
import "fmt" | |
func main() { | |
c1 := make(chan string) | |
c2 := make(chan string) | |
go func() { | |
time.Sleep(time.Second * 1) | |
c1 <- "one" | |
}() | |
go func() { | |
time.Sleep(time.Second * 2) | |
c2 <- "two" | |
}() | |
for i := 0; i < 2; i++ { | |
select { | |
case msg1 := <-c1: | |
fmt.Println("received", msg1) | |
case msg2 := <-c2: | |
fmt.Println("received", msg2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment