When is it safe to block in an Akka 2 actor?

微笑、不失礼 提交于 2020-01-03 04:48:28

问题


I know that it is not recommended to block in the receive method of an actor, but I believe it can be done (as long as it is not done in too many actors at once).

This post suggests blocking in preStart as one way to solve a problem, so presumably blocking in preStart is safe.

However, I tried to block in preRestart (not preStart) and everything seemed to just hang - no more messages were logged as received.

Also, in cases where it is not safe to block, what is a safe alternative?


回答1:


It's relatively safe to block in receive when:

  1. the number of blocked actors in total is much smaller than the number of total worker threads. By default there are ten worker threads, so 1-2 blocked actors are fine

  2. blocking actor has its own, dedicated dispatcher (thread pool). Other actors are not affected

When it's not safe to block, good alternative is to... not block ;-). If you are working with legacy API that is inherently blocking you can either have a separate thread pool maintained inside some actor (feels wrong) or use approach 2. above - dedicate few threads to a subset of actors that need to block.




回答2:


Never ever block an actor.

If your actor is part of an actor hierarchy (and it should be), the actor system is not able to stop it. The actor's life-cycle (supervision, watchig etc.) is done by messaging. Stopping a parent actor of a blocking child will not work.

Maybe there are ways to couple the blocking condition with the actor's lifecycle. But this would lead to overload of complications and bad-style.

So, the best way is to do the blocking part outside of that actor. E.g. you could run the blocking code via an executor service in a separate thread.



来源:https://stackoverflow.com/questions/14505405/when-is-it-safe-to-block-in-an-akka-2-actor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!