javipalanca / spade

Smart Python Agent Development Environment
MIT License
258 stars 98 forks source link

Agent not receiving messages in SPADE #107

Open 1181244 opened 1 year ago

1181244 commented 1 year ago

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 commented 1 year ago

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

javipalanca commented 1 year ago

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