Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent not receiving messages in SPADE #107

Open
1181244 opened this issue Jun 15, 2023 · 2 comments
Open

Agent not receiving messages in SPADE #107

1181244 opened this issue Jun 15, 2023 · 2 comments

Comments

@1181244
Copy link

1181244 commented Jun 15, 2023

  • SPADE version: 3.3.0
  • Python version: 3.11.3
  • Operating System: Windows 10 home 64-bit version

Description

Hello,

I'm currently working on a project where I have two agents set up using the SPADE library: a CoreAgent and an ImageProcessingAgent. The CoreAgent is supposed to send an image (encoded as a base64 string) to the ImageProcessingAgent, which then processes the image and sends back the results.

However, I'm running into an issue where the ImageProcessingAgent does not appear to be receiving any messages from the CoreAgent, even though the CoreAgent seems to successfully send the message. No exceptions or error messages are thrown, the ImageProcessingAgent just keeps waiting for a message indefinitely.

Here is the basic code for both agents:

CoreAgent:

class CoreAgent(Agent):
    class RequestBehaviour(OneShotBehaviour):
        async def run(self):
            img = cv2.imread(self.agent.img_path)
            _, img_encoded = cv2.imencode('.jpg', img)
            img_bytes = img_encoded.tobytes()
            img_str = base64.b64encode(img_bytes).decode()
            msg = Message(to="image@localhost")
            msg.body = img_str
            msg.set_metadata("performative", "inform")
            if self.agent.is_alive():
                print('message sent')
                await self.send(msg)
    # (Other setup and behavior code omitted for brevity)
    async def setup(self):
        print("CoreAgent started")
        receive_behaviour = self.ReceiveBehaviour()
        request_behaviour = self.RequestBehaviour()
        template = Template()
        template.set_metadata("performative", "inform")
        self.add_behaviour(receive_behaviour,template)
        self.add_behaviour(request_behaviour,template)

ImageProcessingAgent:

class ImageProcessingAgent(Agent):
    class ReceiveBehaviour(spade.behaviour.CyclicBehaviour):
        async def run(self):
            msg = await self.receive()
            if msg:
                print('receiving message')
                # (Message processing code omitted for brevity)
    # (Other setup and behavior code omitted for brevity)

    async def setup(self):
        print("ImageProcessingAgent started")
        receiveBehaviour = self.ReceiveBehaviour()
        template = Template()
        template.set_metadata("performative", "inform")
        self.add_behaviour(receiveBehaviour,template)

Both agents are started with:

imageAgent = ImageProcessingAgent("image@localhost", "password")
coreAgent = CoreAgent(img_path, "core@localhost", "password")

async def runAgents():
    future_image = imageAgent.start()
    await future_image
    time.sleep(3)
    future_core = coreAgent.start()
    await future_core

asyncio.run(runAgents())

What I Did

Here is the console output when running the agents:

ImageProcessingAgent started
CoreAgent started
message sent

No further output is produced after the "message sent" line, indicating that the ImageProcessingAgent is not receiving the message. The "receiving message" print statement in the ReceiveBehaviour run method is never executed.

I have double-checked the agent names, ensured both agents are running at the time the message is sent, and there should not be any network issues as this is running on localhost.

I would really appreciate any help on this. Thank you!

@1181244
Copy link
Author

1181244 commented Jun 15, 2023

Downgraded to Python 3.9 to check if it worked, but it made no difference.

@javipalanca
Copy link
Owner

replace time.sleep(3) with await asyncio.sleep(3) to avoid blocking the event-loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants