def do_customEvent(self, reactor_handler, event_root): class CustomHandler: did_custom = False did_init = False def __init__(self, *handlers): self.handlers = handlers def on_reactor_init(self, event): self.did_init = True def on_custom(self, event): self.did_custom = True class CustomInvoker(CustomHandler): def on_reactor_init(self, event): h = event_root(event) event.dispatch(h, CUSTOM) self.did_init = True child = CustomInvoker() root = CustomHandler(child) reactor = Reactor() reactor_handler(reactor, root) reactor.run() assert root.did_init assert child.did_init assert root.did_custom assert child.did_custom
def main(): parser = ArgumentParser() parser.add_argument("-n", "--host", default="127.0.0.1", help="network hostname") parser.add_argument("-p", "--port", default="5680", help="network port") args = parser.parse_args() Reactor(BizLogic(args)).run()
def test_reactorHandlerCycling(self, n=0): class CustomHandler(Handler): UNSET = 999999999 def __init__(self): self.offset = len(traceback.extract_stack()) def on_reactor_init(self, event): self.depth = len(traceback.extract_stack()) def reset(self): self.depth = self.UNSET @property def init_depth(self): d = self.depth - self.offset return d custom = CustomHandler() reactor = Reactor() reactor.handler = custom for i in range(n): h = reactor.handler reactor.handler = h custom.reset() reactor.run() assert custom.init_depth < 50, "Unexpectedly long traceback for a simple handler"
def __init__(self, *args): self.reactor = Reactor(*args) self.reactor.global_handler = self self.io = IOHandler() self.loop = tornado.ioloop.IOLoop.instance() self.count = 0 self.reactor.start() self.reactor.process()
def main(): parser = ArgumentParser() parser.add_argument("-n", "--host", default="127.0.0.1", help="network hostname") parser.add_argument("-p", "--port", default="6000", help="network port") parser.add_argument("-l", "--level", default="warning", help="logging level") args = parser.parse_args() logging.getLogger().setLevel(getattr(logging, args.level.upper())) Reactor(Monitor(args)).run()
def main(): parser = ArgumentParser() parser.add_argument("rate", type=float, help="Barks per second") parser.add_argument("-n", "--host", default="127.0.0.1", help="hostname of outboxes") args = parser.parse_args() Reactor(AutoBark(args.rate, args.host)).run()
def protonj_to_protonc(self, count): if (not self.proton_j_available): raise Skipped("ProtonJ not found") rh = ReceiveHandler(count) r = Reactor(rh) r.run() rh.java_thread.join() assert (rh.java_thread.result == 0) for i in range(1, count): assert (rh.messages[i - 1] == ("message-" + str(i)))
def main(): parser = ArgumentParser(prog="listen") parser.add_argument("user") parser.add_argument("-n", "--host", default="127.0.0.1", help="hostname of inboxes") args = parser.parse_args() users = common.load_data("users.pickle") assert args.user in users Reactor(GetBarks(args.user, args.host)).run()
def protonc_to_protonj(self, count): if (not self.proton_j_available): raise Skipped("ProtonJ not found") port = free_tcp_port() java_thread = JavaThread("recv", port, count) java_thread.start() # Give the Java thread time to spin up a JVM and start listening # XXX: would be better to parse the stdout output for a message time.sleep(1) sh = SendHandler('127.0.0.1:' + str(port), count) r = Reactor(sh) r.run() java_thread.join() assert (java_thread.result == 0)
dlv = snd.send(self.message) dlv.settle() snd.close() snd.session.close() snd.connection.close() class Program: def __init__(self, url, content): self.url = url self.content = content def on_reactor_init(self, event): # You can use the connection method to create AMQP connections. # This connection's handler is the Send object. All the events # for this connection will go to the Send object instead of # going to the reactor. If you were to omit the Send object, # all the events would go to the reactor. event.reactor.connection_to_host( self.url.host, self.url.port, Send(Message(self.content), self.url.path)) args = sys.argv[1:] url = Url(args.pop() if args else "localhost:5672/examples") content = args.pop() if args else "Hello World!" r = Reactor(Program(url, content)) r.run()
class Logger: def on_unhandled(self, name, event): print("LOG:", name, event) class Task: def on_timer_task(self, event): print("Mission accomplished!") class Program: def on_reactor_init(self, event): print("Hello, World!") event.reactor.schedule(0, Task()) def on_reactor_final(self, event): print("Goodbye, World!") r = Reactor(Program()) # In addition to having a regular handler, the reactor also has a # global handler that sees every event. By adding the Logger to the # global handler instead of the regular handler, we can log every # single event that occurs in the system regardless of whether or not # there are specific handlers associated with the objects that are the # target of those events. r.global_handler.add(Logger()) r.run()
def setUp(self): self.sink = Sink() self.server = Server(self.sink) self.reactor = Reactor(self.server)
def setUp(self): self.source = Source("test-%s") self.server = Server(self.source) self.reactor = Reactor(self.server)
print "LOG:", name, event class Program: def on_reactor_init(self, event): print "Hello, World!" def on_reactor_final(self, event): print "Goodbye, World!" # You can pass multiple handlers to a reactor when you construct it. # Each of these handlers will see every event the reactor sees. By # combining this with on_unhandled, you can log each event that goes # to the reactor. r = Reactor(Program(), Logger()) r.run() # Note that if you wanted to add the logger later, you could also # write the above as below. All arguments to the reactor are just # added to the default handler for the reactor. def logging_enabled(): return False r = Reactor(Program()) if logging_enabled(): r.handler.add(Logger()) r.run()
#!/usr/bin/python import time from proton.reactor import Reactor class World: def on_reactor_init(self, event): print "World!" class Goodbye: def on_reactor_final(self, event): print "Goodbye, World!" class Hello: def __init__(self): self.handlers = [World(), Goodbye()] # The parent handler always receives the event first. def on_reactor_init(self, event): print "Hello", r = Reactor(Hello()) r.run()
def setUp(self): fake_tether = Tether(None, "", None) self.agent = Agent(fake_tether, self) self.server = Server(self.agent) self.reactor = Reactor(self.server) self.samples = 0
def setUp(self): import platform if platform.python_implementation() != "Jython": # Exception propagation does not work currently for CPython raise SkipTest() self.reactor = Reactor()
from __future__ import print_function import time from proton.reactor import Reactor # Events know how to dispatch themselves to handlers. By combining # this with on_unhandled, you can provide a kind of inheritance # between handlers using delegation. class Hello: def on_reactor_init(self, event): print("Hello, World!") class Goodbye: def on_reactor_final(self, event): print("Goodbye, World!") class Program: def __init__(self, *delegates): self.delegates = delegates def on_unhandled(self, name, event): for d in self.delegates: event.dispatch(d) r = Reactor(Program(Hello(), Goodbye())) r.run()
snd = event.sender if snd.credit > 0: dlv = snd.send(self.message) dlv.settle() snd.close() snd.session.close() snd.connection.close() class Program: def __init__(self, hostname, content): self.hostname = hostname self.content = content def on_reactor_init(self, event): # You can use the connection method to create AMQP connections. # This connection's handler is the Send object. All the events # for this connection will go to the Send object instead of # going to the reactor. If you were to omit the Send object, # all the events would go to the reactor. event.reactor.connection(Send(self.hostname, Message(self.content))) args = sys.argv[1:] hostname = args.pop() if args else "localhost" content = args.pop() if args else "Hello World!" r = Reactor(Program(hostname, content)) r.run()
def setUp(self): self.reactor = Reactor()
def on_reactor_init(self, event): # You can use the connection method to create AMQP connections. # This connection's handler is the Send object. All the events # for this connection will go to the Send object instead of # going to the reactor. If you were to omit the Send object, # all the events would go to the reactor. event.reactor.connection_to_host(self.url.host, self.url.port, Perfy(self.node, self.count)) _usage = """Usage: %prog [options]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="server", type="string", default="amqp://0.0.0.0:5672", help="The address of the server [amqp://0.0.0.0:5672]") parser.add_option("--node", type='string', default='amq.topic', help='Name of source/target node') parser.add_option("--count", type='int', default=100, help='Send N messages (send forever if N==0)') opts, _ = parser.parse_args(args=sys.argv) r = Reactor(Program(Url(opts.server), opts.node, opts.count)) r.run()