1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#!/usr/bin/env python
"""
Bridge Example
A Bridge example that demonstrates bidirectional parent/child
communications and displays the no. of events per second and latency.
"""
import sys
from signal import SIGINT, SIGTERM
from time import time
from traceback import format_exc
from circuits import Component, Event, handler, ipc
def log(msg, *args, **kwargs):
sys.stderr.write('{:s}{:s}'.format(msg.format(*args), kwargs.get('n', '\n')))
sys.stderr.flush()
def error(e):
log('ERROR: {0:s}', e)
log(format_exc())
def status(msg, *args):
log('\r\x1b[K{0:s}', msg.format(*args), n='')
class ping(Event):
"""ping Event"""
class pong(Event):
"""pong Event"""
class Child(Component):
def ping(self, ts):
self.fire(ipc(pong(ts, time())))
class App(Component):
def init(self):
self.events = 0
self.stime = time()
Child().start(process=True, link=self)
def ready(self, *args):
self.fire(ipc(ping(time())))
def pong(self, ts1, ts2):
latency = (ts2 - ts1) * 1000.0
status(
f'{int(self.events / (time() - self.stime)):d} event/s @ {latency:0.2f}ms latency',
)
self.fire(ipc(ping(time())))
def signal(self, signo, stack):
if signo in [SIGINT, SIGTERM]:
raise SystemExit(0)
@handler()
def on_event(self, *args, **kwargs):
self.events += 1
App().run()
|