Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shows how to implement a SimPy-like simulation using timeouts and generators.
from collections import defaultdict
def blinker(env):
while True:
print 'Blink at {}!'.format(
yield 5
class BlinkerEnvironment(object):
def __init__(self, blinkers=4): = 0
self.blinkers = defaultdict(list)
for idx in xrange(blinkers):
# blinkers are scheduled by what they yield.
def run(self, until=100):
while < until:
if in self.blinkers:
for blinker in self.blinkers.pop(
timeout = +
self.blinkers[timeout].append(blinker) = min(self.blinkers.keys())
if __name__ == "__main__":
env = BlinkerEnvironment()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment