- commit
- 2799c07ad375c928777038c468060a58b4b05080
- parent
- a3a3a4c9b057e17cf5f30e5e46403f979f58bd05
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-10-18 08:33
implement LoopingCall this was previously implemented as part of twisted
Diffstat
| M | laneya/client.py | 2 | +- |
| M | laneya/protocol.py | 27 | +++++++++++++++++++++++++++ |
| M | laneya/server.py | 2 | +- |
3 files changed, 29 insertions, 2 deletions
diff --git a/laneya/client.py b/laneya/client.py
@@ -51,7 +51,7 @@ def main(): 51 51 endpoint = TCP4ClientEndpoint(reactor, 'localhost', 5001) 52 52 endpoint.connect(client) 53 5354 -1 mainloop = task.LoopingCall(client.mainloop)-1 54 mainloop = protocol.LoopingCall(loop, client.mainloop) 55 55 mainloop.start(0.02) 56 56 57 57 reactor.run()
diff --git a/laneya/protocol.py b/laneya/protocol.py
@@ -92,6 +92,33 @@ def generate_key(): 92 92 return key 93 93 94 94 -1 95 class LoopingCall(object): -1 96 def __init__(self, loop, fn, *args, **kwargs): -1 97 self.loop = loop -1 98 self.fn = fn -1 99 self.args = args -1 100 self.kwargs = kwargs -1 101 self.running = False -1 102 self.interval = None -1 103 -1 104 def start(self, interval, now=True): -1 105 self.interval = interval -1 106 self.running = True -1 107 -1 108 if now: -1 109 self._wrapped() -1 110 else: -1 111 self.loop.call_later(self.interval, self._wrapped) -1 112 -1 113 def stop(self): -1 114 self.running = False -1 115 -1 116 def _wrapped(self): -1 117 if self.running: -1 118 self.loop.call_later(self.interval, self._wrapped) -1 119 self.fn(*self.args, **self.kwargs) -1 120 -1 121 95 122 class JSONProtocol(NetstringReceiver): 96 123 """Send and receive JSON objects.""" 97 124
diff --git a/laneya/server.py b/laneya/server.py
@@ -70,7 +70,7 @@ def main(): 70 70 endpoint = TCP4ServerEndpoint(reactor, 5001) 71 71 endpoint.listen(server) 72 7273 -1 mainloop = task.LoopingCall(server.mainloop)-1 73 mainloop = protocol.LoopingCall(loop, server.mainloop) 74 74 mainloop.start(0.1) 75 75 76 76 reactor.run()