Skip to content

Instantly share code, notes, and snippets.

@encukou
Created June 6, 2014 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encukou/7013fadae2686d737ccd to your computer and use it in GitHub Desktop.
Save encukou/7013fadae2686d737ccd to your computer and use it in GitHub Desktop.
asyncio blinkers
#! /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