Created
April 27, 2017 15:28
-
-
Save edouardklein/f20d9648fad4ca1f7c825c429d8afa36 to your computer and use it in GitHub Desktop.
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
# package main | |
# import ( | |
# "bufio" | |
# "time" | |
# "fmt" | |
# "os" | |
# ) | |
import sys | |
import time | |
import asyncio | |
def coroutinize(f, *args, **kwargs): | |
return asyncio.get_event_loop().run_in_executor(None, | |
lambda: f(*args, | |
**kwargs)) | |
def go(f): | |
return asyncio.ensure_future(f) | |
# func worker(c chan string) { | |
# line := <-c | |
# fmt.Println(line) | |
# } | |
async def worker(c): | |
print("Worker waiting on channel") | |
line = await c.get() | |
print("Worker just read {}".format(line)) | |
# func readLine(c chan string) { | |
# reader := bufio.NewReader(os.Stdin) | |
# line, err := reader.ReadString('\n') | |
# fmt.Println(err) | |
# c <- line | |
# } | |
async def read_line(c): | |
print("Read line waiting on stdin") | |
line = await coroutinize(sys.stdin.readline) | |
print("Read line sending '{}' to channel".format(line)) | |
await c.put(line) | |
# func doWork(end chan int) { | |
# time.Sleep(2000 * time.Millisecond) | |
# fmt.Println("do_work") | |
# end <- 1 | |
# } | |
async def do_work(end): | |
print("Do work starts working") | |
await coroutinize(time.sleep, 2) | |
print("Do work sends somehting on the end channel") | |
await end.put(1) | |
# func main() { | |
# end := make(chan int) | |
# c := make(chan string) | |
# go worker(c) | |
# go readLine(c) | |
# go doWork(end) | |
# <-end | |
# } | |
async def main(): | |
end = asyncio.Queue(1) | |
c = asyncio.Queue(1) | |
print("Main launches worker") | |
go(worker(c)) | |
print("Main launches readline") | |
go(read_line(c)) | |
print('Main launches do_work') | |
go(do_work(end)) | |
print("Main waits on end.") | |
await end.get() | |
print("Main quits.") | |
if __name__ == '__main__': | |
print("Bullshit code gets the event loop") | |
loop = asyncio.get_event_loop() | |
print("Bullshit code starts the event loop") | |
loop.run_until_complete(main()) | |
print("Bullshit code exits") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment