erlang - 发送大消息性能 [英] erlang - sending big message performance
问题描述
模块(chain_hello)。
start(N,Some_big_data) - >
Pid1 = spawn(chain_hello,some_fun,[N]),
Pid1! Some_big_data,
io:format(done \\\
)。
尽管 Some_big_data
是一个非常大的参考数据(例如文件的内容) - 发送时是否复制?对于性能有很大的惩罚吗?
通常我会使用一些线程安全的共享对象(和/或互斥体)。在Erlang中是否有解决方案来避免复制邮件内容?
ADDED:
有趣的情况是Some_big_data是
ADDED2
好的,我看到Erlang没有这样的解决方案(剪切一些结构化数据,如工作进程中的地图) - 因为Erlang设计。但是,我认为这是坚实的工作,容易的并行管理是合理的。
作为建议,您只能将当前进程(self())的pid发送到所需的进程来处理那个some_big_data。这样,当你想要使用那个some_big_data时,你可以从第二个进程中引用它。
例如:
-module(proc1)。
send_big_data() - >
BigData = get_some_big_data(),
Pid = spawn(fun proc2:start / 0),
Pid! {process_big_data,self()},
loop(Pid,BigData)。
loop(Pid,BigData) - >
收到
get_first_element - >
[H | T] = BigData,
Pid! {response,H};
_ - >
P! {nothing}
end,
loop(Pid)。
(对于最终的语法错误,抱歉)。
suppose I create new local process in an erlang application, and i want to send to it a big message.
-module(chain_hello).
start(N, Some_big_data)->
Pid1 = spawn(chain_hello, some_fun, [N]),
Pid1 ! Some_big_data,
io:format("done \n").
despite Some_big_data
is a reference to really big data (eg. content of a file) - is it copied when sending? Are there big penalties for performance?
Normally I would use some thread safe shared object (and/or mutex). Is there any solution in Erlang to avoid copying message content?
ADDED:
Interesting case is when Some_big_data is structured content - to be specific: map, on which I can perform some operations.
ADDED2
Ok, I see there is no such solution for Erlang (shearing some structured data like map in worker process) - because of Erlang design. But I think it is justified by solid work, and easily concurrence management.
As a suggestion, you can send only the pid of the current process (self()) to the process that you want to handle that some_big_data. That way, when you want to use that some_big_data, you can reference it back to it from the 2nd process.
For instance:
-module(proc1).
send_big_data() ->
BigData = get_some_big_data(),
Pid = spawn(fun proc2:start/0),
Pid ! {process_big_data, self()},
loop(Pid, BigData).
loop(Pid,BigData) ->
receive
get_first_element ->
[H|T] = BigData,
Pid ! {response, H};
_ ->
Pid ! {nothing}
end,
loop(Pid).
(Sorry for the eventual syntax mistakes).
这篇关于erlang - 发送大消息性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!