Permalink
Browse files

Added coroutines to start agents from within other agents.

Improved API doc format.
  • Loading branch information...
javipalanca committed Sep 20, 2018
1 parent 7f37bc6 commit f8214f128f1a0d200ad4b8f0ced4c8b23f01987b
Showing with 549 additions and 301 deletions.
  1. +3 −1 README.rst
  2. +1 −1 docs/agents.rst
  3. +1 −1 docs/extending.rst
  4. +0 −1 docs/spade.rst
  5. +24 −0 docs/usage.rst
  6. +136 −60 spade/agent.py
  7. +134 −103 spade/behaviour.py
  8. +61 −28 spade/message.py
  9. +88 −47 spade/presence.py
  10. +10 −6 spade/template.py
  11. +35 −28 spade/trace.py
  12. +25 −25 spade/web.py
  13. +29 −0 tests/test_agent.py
  14. +2 −0 tests/utils.py
View
@@ -51,7 +51,9 @@ Features
Plugins
-------
None yet.
- **spade_bokeh** (bokeh plots for agents):
- Code: https://github.com/javipalanca/spade_bokeh
- Documentation: https://spade-bokeh.readthedocs.io/
Credits
---------
View
@@ -13,7 +13,7 @@ attributes defined in the template must be equal in the message for this to matc
The attributes that can be set in a template are:
* **to**: the jid string of the receiver of the message.
* **from** the jid string of the sender of the message.
* **sender** the jid string of the sender of the message.
* **body**: the body of the message.
* **thread**: the thread id of the conversation.
* **metadata**: a (key, value) dictionary of strings to define metadata of the message. This is useful, for example, to include `FIPA <http://www.fipa.org>`_ attributes like *ontology*, *performative*, *language*, etc.
View
@@ -92,7 +92,7 @@ To develop this example mixin you should do the following::
await super().send(msg)
async def senf_p2p(self, msg):
async def send_p2p(self, msg):
...
View
@@ -59,4 +59,3 @@ spade.web module
:members:
:undoc-members:
:show-inheritance:
View
@@ -204,3 +204,27 @@ And the output of this example would be::
.. hint::
If a exception occurs inside an ``on_start``, ``run`` or ``on_end`` coroutines, the behaviour will be
automatically killed and the exception will be stored as its ``exit_code``.
Creating an agent from within another agent
-------------------------------------------
There is a common use case where you may need to create an agent from within another agent, that is, from within another
agent's behaviour. This is a *special* case because you can't create a new event loop when you have a loop already
running. For this special case you can use the ``loop`` argument in the ``Agent`` constructor to share an event loop
between more than one agent. There is also a coroutine that allows you to start the agent from a behaviour. This is the
``async_start`` coroutine, which accepts the same arguments as the ``start`` method. Example::
class CreateBehav(OneShotBehaviour):
async def run(self):
agent2 = Agent("agent2@fake_server", "fake_password", loop=self.agent.loop)
await agent2.async_start(auto_register=False)
agent1 = Agent("agent1@fake_server", "fake_password")
agent1.add_behaviour(CreateBehav())
agent1.start(auto_register=False)
.. warning:: If you call the ``start`` method (instead of the ``async_start`` coroutine) from within a behaviour, you'll
get an error.
Oops, something went wrong.

0 comments on commit f8214f1

Please sign in to comment.