osbrain.core
— osBrain core logic¶
Core agent classes.
-
class
osbrain.core.
Agent
(name=None, host=None)¶ Bases:
object
A base agent class which is to be served by an AgentProcess.
An AgentProcess runs a Pyro multiplexed server and serves one Agent object.
Parameters: - name : str, default is None
Name of the Agent.
- host : str, default is None
Host address where the agent will bind to. When not set, ‘127.0.0.1’ (localhost) is used.
Attributes: - name : str
Name of the agent.
- host : str
Host address where the agent is binding to.
- socket : dict
A dictionary in which the key is the address or the alias and the value is the actual socket.
- adddress : dict
A dictionary in which the key is the alias and the value is the actual address.
- handler : dict
A dictionary in which the key is the socket and the values are the handlers for each socket.
- poll_timeout : int
Polling timeout, in milliseconds. After this timeout, if no message is received, the agent executes de iddle() method before going back to polling.
- keep_alive : bool
When set to True, the agent will continue executing the main loop.
- running : bool
Set to True if the agent is running (executing the main loop).
Methods
addr
(alias)Parameters: bind
(kind[, alias, handler, host, port])Bind to an agent address. connect
(server_address[, alias, handler])Connect to a server agent address. each
(period, method, *args, **kwargs)Execute a repeated action with a defined period. handle_loopback
(message)Handle incoming messages in the loopback socket. iddle
()This function is to be executed when the agent is iddle. iterate
()Agent’s main iteration. log_debug
(message[, logger])Log a debug message. log_error
(message[, logger])Log an error message. log_info
(message[, logger])Log an info message. log_warning
(message[, logger])Log a warning message. loop
()Agent’s main loop. loopback
(header[, data])Send a message to the loopback socket. on_init
()This user-defined method is to be executed after initialization. ping
()A simple ping method testing purposes. raise_exception
()Raise an exception (for testing purposes). run
()Run the agent. safe
(method, *args, **kwargs)A safe call to a method. safe_ping
()A simple loopback ping for testing purposes. set_attr
(**kwargs)Set object attributes. set_logger
(logger[, alias])Connect the agent to a logger and start logging messages to it. set_loop
(loop)set_method
(*args, **kwargs)Set object methods. stop
()Stop the agent. subscribe
(alias, handlers)Subscribe the agent to another agent. test
()A test method to check the readiness of the agent. close_sockets execute get_attr kill recv register registered self_execute send send_recv set_handler shutdown str2bytes -
addr
(alias)¶ Parameters: - alias : str
Alias of the socket whose address is to be retreived.
Returns: - AgentAddress
Address of the agent socket associated with the alias.
-
bind
(kind, alias=None, handler=None, host=None, port=None)¶ Bind to an agent address.
Parameters: - kind : str, AgentAddressKind
The agent address kind: PUB, REQ…
- alias : str, default is None
Optional alias for the socket.
- handler, default is None
If the socket receives input messages, the handler/s is/are to be set with this parameter.
- host : str, default is None
The host to bind to, when not given self.host is taken as default.
- port : int, default is None
An optional port number. If not set, a random port is used for binding.
-
close_sockets
()¶
-
connect
(server_address, alias=None, handler=None)¶ Connect to a server agent address.
Parameters: - server_address : AgentAddress
Agent address to connect to.
- alias : str, default is None
Optional alias for the new address.
- handler, default is None
If the new socket receives input messages, the handler/s is/are to be set with this parameter.
-
each
(period, method, *args, **kwargs)¶ Execute a repeated action with a defined period.
Parameters: - period : float
Repeat the action execution with a delay of period seconds between executions.
- method
Method (action) to be executed by the agent.
- *args : tuple
Parameters to pass for the method execution.
- **kwargs : dict
Named parameters to pass for the method execution.
-
execute
(function, *args, **kwargs)¶
-
get_attr
(name)¶
-
handle_loopback
(message)¶ Handle incoming messages in the loopback socket.
-
iddle
()¶ This function is to be executed when the agent is iddle.
After a timeout occurs when the agent’s poller receives no data in any of its sockets, the agent may execute this function.
-
iterate
()¶ Agent’s main iteration.
This iteration is normally executed inside the main loop.
The agent is polling all its sockets for input data. It will wait for poll_timeout; after this period, the method iddle will be executed before polling again.
Returns: - int
1 if an error occurred during the iteration (we would expect this to happen if an interruption occurs during polling).
0 otherwise.
-
kill
()¶
-
log_debug
(message, logger='_logger')¶ Log a debug message.
Parameters: - message : str
Message to log.
- logger : str
Alias of the logger.
-
log_error
(message, logger='_logger')¶ Log an error message.
Parameters: - message : str
Message to log.
- logger : str
Alias of the logger.
-
log_info
(message, logger='_logger')¶ Log an info message.
Parameters: - message : str
Message to log.
- logger : str
Alias of the logger.
-
log_warning
(message, logger='_logger')¶ Log a warning message.
Parameters: - message : str
Message to log.
- logger : str
Alias of the logger.
-
loop
()¶ Agent’s main loop.
This loop is executed until the keep_alive attribute is False or until an error occurs.
-
loopback
(header, data=None)¶ Send a message to the loopback socket.
-
on_init
()¶ This user-defined method is to be executed after initialization.
-
ping
()¶ A simple ping method testing purposes.
-
raise_exception
()¶ Raise an exception (for testing purposes).
-
recv
(address)¶
-
register
(socket, address, alias=None, handler=None)¶
-
registered
(address)¶
-
run
()¶ Run the agent.
-
safe
(method, *args, **kwargs)¶ A safe call to a method.
A safe call is simply sent to be executed by the main thread.
Parameters: - method : str
Method name to be executed by the main thread.
- *args : arguments
Method arguments.
- *kwargs : keyword arguments
Method keyword arguments.
-
safe_ping
()¶ A simple loopback ping for testing purposes.
-
self_execute
(function, *args, **kwargs)¶
-
send
(address, message, topic='')¶
-
send_recv
(address, message)¶
-
set_attr
(**kwargs)¶ Set object attributes.
Parameters: - kwargs : [name, value]
Keyword arguments will be used to set the object attributes.
-
set_handler
(socket, handler)¶
-
set_logger
(logger, alias='_logger')¶ Connect the agent to a logger and start logging messages to it.
-
set_loop
(loop)¶
-
set_method
(*args, **kwargs)¶ Set object methods.
Parameters: - args : [function]
New methods will be created for each function, taking the same name as the original function.
- kwargs : [name, function]
New methods will be created for each function, taking the name specified by the parameter.
Returns: - str
Name of the registered method in the agent.
-
shutdown
()¶
-
stop
()¶ Stop the agent. Agent will stop running.
-
str2bytes
(message)¶
-
subscribe
(alias, handlers)¶ Subscribe the agent to another agent.
Parameters: - alias : str
Alias of the new subscriber socket.
- handlers : dict
A dictionary in which the keys represent the different topics and the values the actual handlers. If ,instead of a dictionary, a single handler is given, it will be used to subscribe the agent to any topic.
-
test
()¶ A test method to check the readiness of the agent. Used for testing purposes, where timing is very important. Do not remove.
-
class
osbrain.core.
AgentProcess
(name, nsaddr=None, addr=None, base=<class 'osbrain.core.Agent'>)¶ Bases:
multiprocessing.context.Process
Agent class. Instances of an Agent are system processes which can be run independently.
Attributes: - authkey
daemon
Return whether process is a daemon
exitcode
Return exit code of process or None if it has yet to stop
ident
Return identifier (PID) of process or None if it has yet to start
- name
pid
Return identifier (PID) of process or None if it has yet to start
sentinel
Return a file descriptor (Unix) or handle (Windows) suitable for waiting for process termination.
Methods
is_alive
()Return whether process is alive join
([timeout])Wait until child process terminates run
()Method to be run in sub-process; can be overridden in sub-class sigint_handler
(signal, frame)Handle interruption signals. start
()Start child process terminate
()Terminate process; sends SIGTERM signal or uses TerminateProcess() kill -
kill
()¶
-
run
()¶ Method to be run in sub-process; can be overridden in sub-class
-
sigint_handler
(signal, frame)¶ Handle interruption signals.
-
start
()¶ Start child process
-
osbrain.core.
run_agent
(name, nsaddr=None, addr=None, base=<class 'osbrain.core.Agent'>)¶ Ease the agent creation process.
This function will create a new agent, start the process and then run its main loop through a proxy.
Parameters: - name : str
Agent name or alias.
- nsaddr : SocketAddress, default is None
Name server address.
- addr : SocketAddress, default is None
New agent address, if it is to be fixed.
Returns: - proxy
A proxy to the new agent.