====== Go ======
package main
import ("fmt")
var done = make(chan bool)
var cake = make(chan int)
func produce (firstname string) {
for i := 0; i < 10; i++ {
fmt.Printf("%s make cake %d\n", firstname, i)
cake <- i
}
done <- true
}
func consume (firstname string) {
for {
cake_number := <-cake
fmt.Printf("%s eat cake %d\n", firstname, cake_number)
}
}
func main () {
go consume("Bob")
go produce("Alice")
go consume("Maxime")
<- done
}
====== Python ======
import asyncio
q = asyncio.Queue(maxsize=10)
async def produce(firstname):
for i in range(10):
print("{} make cake {}".format(firstname, i))
await asyncio.sleep(0.1)
await q.put(i)
async def consume(firstname):
while True:
cake_number = await q.get()
print("{} eat cake {}".format(firstname, cake_number))
if __name__ == "__main__":
loop = asyncio.get_event_loop()
tasks = [
asyncio.ensure_future(consume("Bob")),
asyncio.ensure_future(produce("Alice")),
asyncio.ensure_future(consume("Maxime"))
]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()