Toto’s event framework is used to allow external events to affect client requests, or to run scheduled tasks after a specified signal is received. It can be used to send messages to active requests, even between multiple server processes. The event framework can also be used outside of Toto to send messages to running Toto servers.
- class toto.events.EventManager(address=None)¶
Instances will listen on address for incoming events.
- classmethod EventManager.instance()¶
Returns the shared instance of EventManager, instantiating on the first call.
Starts listening for incoming events on EventManager.address.
Add a server located at address. This server will now be included in the recipient list whenever send() is called.
Remove the server located at address from the recipient list for all future calls to send().
Clear the recipient list for all future calls to send.
Reload and shuffle the registered server queue used for round-robin load balancing of non-broadcast events.
- EventManager.register_handler(event_name, event_handler, run_on_main_loop=False, request_handler=None, persist=False)¶
Register event_handler to run when event_name is received. Handlers are meant to respond to a single event matching event_name only. If run_on_main_loop is True the handler will be executed on Tornado’s main IOLoop (required if the handler will write to a response stream). If request_handler is set, event_handler will not fire once request_handler has finished. Set persist to True to automatically requeue event_handler each time it is executed.
Disable and remove the handler matching handler_sig.
- EventManager.send_to_server(address, event_name, event_args)¶
Send a message with event_name and event_args only to the server listening at address. address must have previously been passed to register_server. This is more efficient than send if you only intent to send the event to a single server and know the address in advance.
- EventManager.send(event_name, event_args, broadcast=True)¶
Send a message with event_name and event_args to all servers previously registered with register_server(). If broadcast is false, the event will be sent to only a single server. Non-broadcast events are round-robin load balanced between registered servers.