Created
June 6, 2014 12:37
-
-
Save encukou/7013fadae2686d737ccd to your computer and use it in GitHub Desktop.
asyncio blinkers
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
#! /usr/bin/python3 | |
# for an async presentation at Ostravské Pyvo, 2014-06-05 | |
# - http://srazy.info/pyvo-v-ostrave/4591 | |
# - http://lanyrd.com/2014/ostrava-pyvo-june/ | |
import random | |
import time | |
import asyncio | |
def print_all(): | |
print(*blinkers, sep=' ', end='\r') | |
class Blinker: | |
def __init__(self): | |
self._eyes = '(o.o)' | |
asyncio.Task(self.run()) | |
def __str__(self): | |
return self._eyes | |
def set_eyes(self, new): | |
self._eyes = new | |
print_all() | |
@asyncio.coroutine | |
def run(self, task=None): | |
while True: | |
self.set_eyes('(o.o)') | |
yield from asyncio.sleep(random.expovariate(1/3)) | |
self.set_eyes('(-.-)') | |
yield from asyncio.sleep(random.uniform(0.05, 0.3)) | |
blinkers = [Blinker() for i in range(10)] | |
print_all() | |
asyncio.get_event_loop().run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment