Skip to content

Commit

Permalink
Converted forgotten example in doc to doctest.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcschrg committed Oct 17, 2024
1 parent 030fe6f commit 51e2449
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,25 @@ Connecting two agents
We can now connect an instance of a HelloWorldAgent with an instance of
a RepeatingAgent and let them run.

.. code-block:: python
.. testcode::

import asyncio
from mango import Agent, create_tcp_container, activate


class RepeatingAgent(Agent):
def __init__(self, container):
# We must pass a ref. to the container to "mango.Agent":
super().__init__(container)
print(f"Hello world! My id is {self.aid}.")
def __init__(self):
super().__init__()
print(f"Creating a RepeatingAgent. At this point self.addr={self.addr}")

def handle_message(self, content, meta):
# This method defines what the agent will do with incoming messages.
print(f"Received a message with the following content: {content}")
print(f"Received a message with the following content: {content}!")

def on_register(self):
print(f"The agent has been registered to a container: {self.addr}!")

def on_ready(self):
print("All containers have been activated!")

class HelloWorldAgent(Agent):
async def greet(self, other_addr):
Expand All @@ -191,16 +195,17 @@ a RepeatingAgent and let them run.

async with activate(first_container, second_container) as cl:
await second_agent.greet(first_agent.addr)
await asyncio.sleep(.1)

asyncio.run(run_container_and_two_agents(
first_addr=('localhost', 5555), second_addr=('localhost', 5556))
)

def test_second_example():
asyncio.run(run_container_and_two_agents(
first_addr=('localhost', 5555), second_addr=('localhost', 5556))
)
You should now see the following output:
.. testoutput::

`Hello world! My id is agent0.`
`Received a message with the following content: Hello world!`
Creating a RepeatingAgent. At this point self.addr=None
The agent has been registered to a container: AgentAddress(protocol_addr=('localhost', 5555), aid='agent0')!
All containers have been activated!
Received a message with the following content: Hello world!!

You have now successfully created two agents and connected them.

0 comments on commit 51e2449

Please sign in to comment.