2 ms·
You can get channel-like behavior using async/await and a custom event loop. I wrote a little example[1] that has a bare-bones implementation of this. I used it
by e271828 11y ago
You can get channel-like behavior using async/await and a custom event loop. I wrote a little example[1] that has a bare-bones implementation of this. I used it to translate the prime sieve example from Go[2] almost directly to Python. The code uses "message = await channel.receive()" to mimic Go's "message <- channel". Instead of using "go func()" to fire off a goroutine, I use loop.run(func()) to add the PEP492 coroutine to my simple event loop.
It's not an efficient implementation - it was really meant as a proof of concept that you can use async/await in your own code without any reference to asyncio.
[1] https://gist.github.com/vrajivk/c505310fb79d412afcd5#file-sieve-py https://gist.github.com/vrajivk/c505310fb79d412afcd5#file-si...
https://gist.github.com/vrajivk/c505310fb79d412afcd5#file-channel-py https://gist.github.com/vrajivk/c505310fb79d412afcd5#file-ch...
[2] https://golang.org/doc/play/sieve.go https://golang.org/doc/play/sieve.go