如何实现没有Akka的actor模型? [英] How to implement actor model without Akka?

查看:244
本文介绍了如何实现没有Akka的actor模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现没有Akka的简单actors?我不需要高性能许多(非固定计数)actor实例,绿线程,IoC(生命周期,基于Props的工厂,ActorRef的),监督,背压等。只需要顺序(队列)+处理程序+状态+消息传递。

How to implement simple actors without Akka? I don't need high-performance for many (non-fixed count) actor instances, green-threads, IoC (lifecycle, Props-based factories, ActorRef's), supervising, backpressure etc. Need only sequentiality (queue) + handler + state + message passing.

作为副作用,我实际上需要小的基于actor的管道(使用递归链接)+一些parallell演员来优化 DSP 算法计算。它将在库内没有传递依赖,所以我不想(而不能作为一个jar插件)推送用户创建和传递 akkaSystem ,库应该有尽可能简单和轻量级的接口。我不需要IoC,因为它只是一个库(函数集),而不是一个框架 - 所以它有更多的算法复杂性比结构。然而,我认为演员是描述协议的好工具,我实际上可以将算法分解为少量的异步交互实体,因此它符合我的需要。

As a side-effect I actually need small actor-based pipeline (with recursive links) + some parallell actors to optimize the DSP algorithm calculation. It will be inside library without transitive dependencies, so I don't want (and can't as it's a jar-plugin) to push user to create and pass akkaSystem, the library should have as simple and lightweight interface as possible. I don't need IoC as it's just a library (set of functions), not a framework - so it has more algorithmic complexity than structural. However, I see actors as a good instrument for describing protocols and I actually can decompose the algorithm to small amount of asynchronously interacting entities, so it fits to my needs.

为什么不是Akka

Akka意义重大:


  • 这是一个外部依赖;

  • 具有复杂的界面和实现;

  • 对库的用户不透明,例如 - 所有实例由akka的IoC管理,因此不能保证一个逻辑actor总是由同一个实例维护,重新启动将创建一个新的一个;

  • 需要额外的迁移支持,这与scala的迁移支持本身相当。

  • 使用 jstack / 调试akka的绿色线程可能比较困难jconsole
  • it's an external dependency;
  • has complex interface and implementation;
  • non-transparent for library's user, for example - all instances are managed by akka's IoC, so there is no guarantee that one logical actor is always maintained by same instance, restart will create a new one;
  • requires additional support for migration which is comparable with scala's migration support itself.
  • It also might be harder to debug akka's green threads using jstack/jconsole/jvisualvm, as one actor may act on any thread.

当然, ,Akka的jar(1.9Mb)和内存消耗(每GB的250万actors)并不重,所以你甚至可以在Android上运行它。但也知道你应该使用专门的工具来观察和分析演员(例如Typesafe Activator / Console),用户可能不熟悉它(我不会推动他们学习它)。这对于企业项目来说是很好的,因为它几乎总是具有IoC,一些专门的工具和连续迁移,但这不是一个简单的库的好方法。

Sure, Akka's jar (1.9Mb) and memory consumption (2.5 million actors per GB) aren't heavy at all, so you can run it even on Android. But it's also known that you should use specialized tools to watch and analyze actors (like Typesafe Activator/Console), which user may not be familiar with (and I wouldn't push them to learn it). It's all fine for enterprise project as it almost always has IoC, some set of specialized tools and continuous migration, but this isn't good approach for a simple library.

关于依赖。我没有他们,我不想添加任何(我甚至避免了scalaz,这实际上适合这里一点),因为它会导致重大的维护 - 我必须保持我的简单库

P.S. About dependencies. I don't have them and I don't want to add any (I'm even avoiding the scalaz, which actually fits here a little bit), as it will lead to heavy maintenance - I'll have to keep my simple library up-to-date with Akka.


推荐答案

这是JVM世界中最小最低效的actor,基于Minimalist Scala Viktor Klang的演员:
https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/src/test/scala/com/github/gist/viktorklang/Actor.scala

It is handy and safe in usage but isn't type safe in message receiving and cannot send messages between processes or hosts.

它使用起来很方便和安全,但在消息接收中不是类型安全,并且不能在进程或主机之间发送消息。

Main features:

主要功能:


  • 最简单的FSM类API 3个状态( Stay 成为 Die ): a href =https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/src/test/scala/com/github/gist/viktorklang/Actor.scala#L28-L30 =nofollow> https: //github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/src/test/scala/com/github/gist/viktorklang/Actor.scala#L28-L30

minimalistic error handling - just proper forwading to the default exception handler of executor threads: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/src/test/scala/com/github/gist/viktorklang/Actor.scala#L52-L53

minimalistic错误处理 - 只是适当地转换到执行程序线程的默认异常处理程序: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/src/test/scala/com/github/gist/ viktorklang / Actor.scala#L52-L53

fast async initialization that takes ~200 ns to complete, so no need for additional futures/actors for time consuming actor initialization: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/out0.txt#L447

快速异步初始化需要〜200 ns才能完成,因此不需要额外的期货/对于耗时的actor初始化: https://github.com/plokhotnyuk/actors /blob/41eea0277530f86e4f9557b451c7e34345557ce3/out0.txt#L447

smallest memory footprint, that is ~40 bytes in a passive state (BTW the new String() spends the same amout of bytes in the JVM heap): https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/out0.txt#L449

内存占用最小,在被动状态下约为40字节(BTW < c $ c> new String()在JVM堆中花费相同的字节数): https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/out0.txt#L449

very efficient in message processing with throughput ~90M msg/sec for 4 core CPU: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/out0.txt#L466

对于4核CPU,吞吐量大约为90M msg / sec的消息处理效率非常高: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/out0.txt#L466

very efficient in message sending/receiving with latency ~100 ns: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/out0.txt#L472

在邮件中效率非常高发送/接收延迟〜100 ns: https://github.com/通过批处理参数调整公平性的每个actor调整plokhotnyuk / actors / blob / 41eea0277530f86e4f9557b451c7e34345557ce3 / out0.txt#L472

per actor tuning of fairness by the batch parameter: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/src/test/scala/com/github/gist/viktorklang/Actor.scala#L32

状态计数器的示例:

  def process(self: Address, msg: Any, state: Int): Effect = if (state > 0) { 
     println(msg + " " + state)
     self ! msg
     Become { msg => 
        process(self, msg, state - 1)
     }
  } else Die

  val actor = Actor(self => msg => process(self, msg, 5))

结果:

scala> actor ! "a"
a 5

scala> a 4
a 3
a 2
a 1

这篇关于如何实现没有Akka的actor模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆