|
| 1 | +#!/usr/bin/python |
| 2 | +"""mini-guild |
| 3 | +
|
| 4 | +A simplified version of guild without the full set of capabilities. |
| 5 | +Has actor_methods and late bound methods. |
| 6 | +
|
| 7 | +""" |
| 8 | +class Activity: |
| 9 | + def __init__(self): |
| 10 | + super(Activity, self).__init__() |
| 11 | + self.greenthread = None |
| 12 | + self.active = False |
| 13 | + self.sleeping = False |
| 14 | + # -------------------------- |
| 15 | + def start(self): |
| 16 | + self.greenthread = self.main() |
| 17 | + self.active = True |
| 18 | + return self |
| 19 | + |
| 20 | + def tick(self): |
| 21 | + try: |
| 22 | + live = next(self.greenthread) |
| 23 | + return live |
| 24 | + except StopIteration: |
| 25 | + return False |
| 26 | + # -------------------------- |
| 27 | + def input(self, data): |
| 28 | + pass |
| 29 | + def output(self, data): |
| 30 | + raise Exception("base class `output` called directly - you should link over this") |
| 31 | + # -------------------------- |
| 32 | + def main(self): |
| 33 | + while self.active: |
| 34 | + self.sleeping = True |
| 35 | + yield 1 |
| 36 | + def stop(self): |
| 37 | + self.active = False |
| 38 | + def link(self, methodname, target): |
| 39 | + setattr(self, methodname, target) |
| 40 | + |
| 41 | + |
| 42 | +class Actor(Activity): |
| 43 | + Behaviour = None # Should be a subclass of Activity |
| 44 | + def __init__(self, *args, **argd): |
| 45 | + super(Actor, self).__init__() |
| 46 | + if self.Behaviour is None: |
| 47 | + raise Exception("You do not call Actor directly. Inherit and override Behaviour") |
| 48 | + self.inputqueue = [] |
| 49 | + self._behaviour = (self.Behaviour)(*args, **argd) |
| 50 | + |
| 51 | + def handle_inqueue(self): |
| 52 | + call = self.inputqueue.pop(0) |
| 53 | + func_name, args = call[0], call[1:] |
| 54 | + #print(func_name, args) |
| 55 | + func = getattr(self._behaviour, func_name) |
| 56 | + func(*args) |
| 57 | + |
| 58 | + def main(self): |
| 59 | + self._behaviour.start() |
| 60 | + while self._behaviour.active: |
| 61 | + yield 1 |
| 62 | + while (self.inputqueue and self._behaviour.active): |
| 63 | + self.handle_inqueue() |
| 64 | + if self._behaviour.active and not self._behaviour.sleeping: |
| 65 | + self._behaviour.tick() |
| 66 | + |
| 67 | + def link(self, methodname, target): |
| 68 | + setattr(self._behaviour, methodname, target) |
| 69 | + |
| 70 | + def isactive(self): |
| 71 | + return self._behaviour.active |
| 72 | + def issleeping(self): |
| 73 | + return self._behaviour.sleeping |
| 74 | + |
| 75 | + def input(self, data): |
| 76 | + self.inputqueue.append(("input", data)) |
| 77 | + self._behaviour.sleeping = False |
| 78 | + |
| 79 | + def link(self, *args): |
| 80 | + self.inputqueue.append(("link", *args)) |
| 81 | + self._behaviour.sleeping = False |
| 82 | + |
| 83 | + def stop(self): |
| 84 | + self.inputqueue.append(("stop", )) |
| 85 | + self._behaviour.sleeping = False |
| 86 | + |
| 87 | + |
| 88 | +class Producer(Actor): |
| 89 | + class Behaviour(Activity): |
| 90 | + def __init__(self, message): |
| 91 | + super(Producer.Behaviour, self).__init__() |
| 92 | + self.message = message |
| 93 | + def main(self): |
| 94 | + while True: |
| 95 | + yield 1 |
| 96 | + self.output(self.message) |
| 97 | + |
| 98 | + |
| 99 | +class Consumer(Actor): |
| 100 | + class Behaviour(Activity): |
| 101 | + def __init__(self): |
| 102 | + super(Consumer.Behaviour, self).__init__() |
| 103 | + self.count = 0 |
| 104 | + def input(self, data): |
| 105 | + self.count += 1 |
| 106 | + print(data, self.count) |
| 107 | + self.sleeping = True |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + |
| 112 | + p = Producer("Hello").start() |
| 113 | + c = Consumer().start() |
| 114 | + |
| 115 | + p.link("output", c.input) |
| 116 | + |
| 117 | + x = 0 |
| 118 | + |
| 119 | + # Manual scheduler |
| 120 | + |
| 121 | + p.tick() |
| 122 | + c.tick() |
| 123 | + while p.isactive() and c.isactive(): |
| 124 | + p.tick() |
| 125 | + c.tick() |
| 126 | + x +=1 |
| 127 | + if x > 10: |
| 128 | + p.stop() |
| 129 | + c.stop() |
0 commit comments