I saw at concurrency is not parallelism slide that golang can do like this:
func main() {
go boring("Boring!")
fmt.Println("I'm listening.")
time.Sleep(2 * time.Second)
fmt.Println("You're boring; I'm leaving.")
}
The result look like this
I'm listening.
boring 0
boring 1
boring 2
boring 3
boring 4
boring 5
You're boring; I'm leaving.
Can Python async loop do like this? I'm stuck it at loop.run_forever that it will block the main function:
import asyncio
import random
import time
import itertools
async def boring(msg):
for i in itertools.count(0):
print(msg, i)
await asyncio.sleep(random.random() % 1e3)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
asyncio.ensure_future(boring('boring!'))
loop.run_forever()
print('Hello')
time.sleep(2)
print('Bye.')
loop.stop()
It will then run
boring! 0
boring! 1
boring! 2
boring! 3
boring! 4
boring! 5
boring! 6
boring! 7
boring! 8
boring! 9
Can python async loop be async?