简体   繁体   English

Erlang:什么时候产生一个新进程是合乎逻辑的? 什么时候不?

[英]Erlang: When is it logical to spawn a new process? When not?

If we have really heavy-processes system where process spawning is made for some kind of distribution of load - that's clear. 如果我们真的有一个重型流程系统,其中流程产生是为了某种负载分配 - 这是很清楚的。

If we are talking about web-server : it's a good idea to spawn a new proccess for each connection, because then can be distributed. 如果我们谈论web服务器:为每个连接生成一个新进程是个好主意,因为这样可以分发。 But what else? 但还有什么? A single process for Model, View and Controller? 模型,视图和控制器的单一流程? Sounds strange, because they all run in a "liner" way, so it can not be good paralleled and we only get overhead on swapping. 听起来很奇怪,因为它们都是以“班轮”的方式运行,因此不能很好地平行,我们只能在交换时获得开销。 Also, those "Model, View and Controller" are so light, so they can stay in a single process, isn't it? 而且,那些“模型,视图和控制器”是如此轻巧,所以它们可以保持在一个单一的过程中,不是吗?

So, where is it good to spawn a new process excepting "new connection" situation. 因此,除了“新连接”情况之外,产生新进程的好处在哪里。

Thank you in advice. 谢谢你的建议。

In general, it's anywhere you have a shared resource to manage. 通常,您可以在任何地方管理共享资源。 It may be a socket, or a database connection, but it may also be some shared in-memory data, or a state machine of some kind. 它可能是套接字,也可能是数据库连接,但也可能是一些共享的内存中数据或某种状态机。

You may also want to do parallel processing of a list of values (see pmap). 您可能还希望对值列表进行并行处理(请参阅pmap)。

To your "swapping" point you should know that Erlang processes do no use op-sys facilities for scheduling, and scheduling is all but free. 对于您的“交换”点,您应该知道Erlang进程不会使用op-sys工具进行调度,而且调度几乎是免费的。

In the specific case of a web-application server, I understand your question. 在Web应用程序服务器的特定情况下,我理解您的问题。 If you are writing a conventional web application with very little share state. 如果您正在编写具有非常少的共享状态的传统Web应用程序。 Your web framework probably already handles caching and session state and such (these facilities will spawn process). 您的Web框架可能已经处理了缓存和会话状态等(这些工具将生成进程)。

We are all highly indoctrinated into this stateless web application model. 我们都被高度灌输到这种无状态的Web应用程序模型中。 We have all been told since we were pups the stateful systems are hard to develop and they don't scale. 自从我们还是幼犬以来,我们都被告知,有状态系统很难开发,而且无法扩展。 I think you will find that there are those that are challenging that. 我想你会发现那些有挑战性的东西。 As browser support for WebSockets improve, and with server-side language like Erlang and Clojure providing scalable platforms with safe state management, there will be those who are able to make more interactive web-applications. 随着浏览器对WebSockets的支持的提高,以及服务器端语言(如Erlang和Clojure)为可扩展平台提供安全的状态管理,将使那些能够制作更多交互式Web应用程序的人成为可能。 As an extreme example, could you image WoW as a web application? 作为一个极端的例子,您能将WoW成像为Web应用程序吗?

One reason to spawn a new process for each connection is that it makes programming the connections much simpler. 为每个连接生成新进程的一个原因是它使连接编程变得更加简单。 As a process only handles one connection doing things like having blocking access to data-bases, long polling or streaming becomes much easier. 由于一个进程仅处理一个连接,因此会进行诸如阻塞对数据库的访问之类的操作,因此长时间轮询或流式传输变得更加容易。 That this process blocks will not affect any other connections. 该过程阻止不会影响任何其他连接。

In Erlang the general "rule" is that you use processes to model concurrent activity and to manage shared resources. 在Erlang中,一般的“规则”是您使用流程为并发活动建模并管理共享资源。 Processes are the fundamental way for structuring your system. 流程是构建系统的基本方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM