slawlor / ractor

Rust actor framework
MIT License
1.3k stars 66 forks source link

Awaiting input in an actor #235

Closed dns2utf8-novaziun closed 1 month ago

dns2utf8-novaziun commented 2 months ago

Hi all

I am trying to integrate with an mqtt client that has a mqtt.receive().await method that will give me data to parse and handle with the actor.

Currently I can not find a way to call the await from the actor itself without blocking the whole system.

Do I assume correctly that one has to await the handle from (addr, handle) = Actor::spawn(...)?

EDIT: I found the recursion trick where the actor sends itself a message and then awaits connections. However this blocks the actor to one client at the time and messages from other actors are blocked as well. Is there a way to have an actor listen to two inboxes at the same time?

Cheers, Stefan

slawlor commented 1 month ago

You can always spawn them to their own green treads (i.e. tokio:spawn) which they then forward the result to the actor when there is a message available. However you will need to handle panics and task exits yourself, manually.

Actors themselves are incredibly cheap to create, so generally we would create one actor per channel in this case. That actor owns the channel, and does blocking receives (async but blocking the actors message pump) . When the actor receives a message it then forwards it to say an accumulator actor