每个套接字连接使用一个 actor 的 Scala 方法

发布于 2024-08-26 08:27:49 字数 214 浏览 5 评论 0原文

我想知道如何避免一个套接字连接pr。 Scala 中的线程。我想了很多,但最终我总是得到一些代码,这些代码正在侦听每个客户端连接的传入数据。

问题是我想开发一个应用程序,它应该同时处理大约几千个连接。然而,我当然不想为每个连接创建一个线程,因为缺乏可扩展性和上下文切换。

执行此操作的“正确”方法是什么?在我的世界中,每个连接应该可以有一个参与者,而不需要为每个参与者阻塞一个线程。

I am wondering how it is possible to avoid one socket connection pr. thread in Scala. I have thought a lot about it, but I always end up with some code which is listening for incoming data for each client connection.

The problem is that I want to develop an application which should simultanously handle perhaps a couple of thousand connections. However I will of course not want to create a thread for each connection because of the lack of scalability and context switching.

What would be the "right" way to do this. In my world it should be possible to have one actor for each connection without the need to block one thread per actor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-09-02 08:27:50

我有一个将参与者与非阻塞套接字(即NIO)混合在一起的应用程序。我这样做的方法是拥有一个专用的 IO 线程,它使用反应器模式向 actor 发送消息(与将工作委托给 Java 系统中的线程池的方式大致相同)。

显然,使用阻塞套接字,每个连接只能使用一个线程。演员可以处理这个问题,但这当然限制了可以同时处理的连接数量。

在单个 IO 线程的情况下,这在理论上是一个瓶颈,但在实践中(根据我们的观察)并不多,因为 IO 线程正在执行计算非密集型工作。 NIO 上有很多很好的讨论 反应器模式

I have an application that mixes actors with non-blocking sockets (i.e. NIO). The way I have done this is to have a dedicated IO thread, which sends messages to actors (in much the same way it would delegate work to a thread pool in a Java system) using the reactor pattern.

Obviously using the old blocking sockets, you are restricted to one thread per connection. And actor could handle this but of course this places a restriction on the number of connections which can be handled simultaneously.

In the case of a single IO thread, this is a bottleneck in theory but not much in practice (in our observations) as the IO thread is doing computationally non-intensive work. There are plenty of good discussions to be found on the NIO reactor pattern.

爱本泡沫多脆弱 2024-09-02 08:27:49

在《Programming Scala》一书中,作者使用了一个名为 naggati 的库,它提供了一个结合 NIO 和 actor 的框架, http://programming-scala.labs.oreilly.com/ch09.html

In the book "Programming Scala" the authors used a library called naggati which provides a framework that combines NIO and actors, http://programming-scala.labs.oreilly.com/ch09.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文