aiomas.agent¶
This module implements the base class for agents (Agent) and
containers for agents (Container).
Every agent must live in a container. A container can contain one ore more agents. Containers are responsible for making connections to other containers and agents. They also provide a factory function for spawning new agent instances and registering them with the container.
Thus, the Agent base class is very light-weight. It only has a name,
a reference to its container and an RPC router (see aiomas.rpc).
-
class
aiomas.agent.Container(addr, codec=None, clock=None)[source]¶ Container for agents.
It can instantiate new agents via
spawn()and can create connections to other agents (viaconnect()).In order to destroy a container and close all of its sockets, call
shutdown().When a container is created, it also creates a server socket and binds it to addr which is a
('host', port)tuple (see the host and port parameters ofasyncio.BaseEventLoop.create_server()for details).You can optionally also pass a codec instance (
JSONorMsgPack(default)). Containers can only “talk” to containers using the same codec.To decouple a multi-agent system from the system clock, you can pass an optional clock object which should be an instance of
BaseClock. This makes it easier to integrate your system with other simulators that may provide a clock for you or to let your MAS run as fast as possible. By default,AsyncioClockwill be used.-
codec¶ The codec used by this container. Instance of
aiomas.codecs.Codec.
-
clock¶ The clock of the container. Instance of
aiomas.clocks.BaseClock.
-
spawn(agent_type, *args, **kwargs)[source]¶ Create an instance of agent_type, passing a reference to this container, a name and the provided args and kwargs to it.
This is equivalent to
agent = agent_type(container, name, *args, **kwargs), but also registers the agent with the container.
-
connect(url)[source]¶ Connect to the argent available at url and return a proxy to it.
url is a string
agent://<host>:<port>/<agent-name>.
-
shutdown(async=False)[source]¶ Close the container’s server socket and the RPC services for all outgoing TCP connections.
If async is left to
False, this method callsasyncio.BaseEventLoop.run_until_complete()in order to wait until all sockets are closed.If the event loop is already running when you call this method, set async to
True. The return value then is a coroutine that you need toyield fromin order to actually shut the container down:yield from container.shutdown(async=True)
-
-
class
aiomas.agent.Agent(container, name)[source]¶ Base class for all agents.
-
rpc¶ Descriptor that creates an RPC
Routerfor every agent instance.You can override this in a sub-class if you need to. (Usually, you don’t.)
-
name¶ The agent’s name. It is formatted like an agent URL and can usually (but not necessarily) be used to connect to this agent.
-