如何在 Erlang 中使进程并行运行?
startTrains() ->
TotalDist = 100,
Trains = [trainA,trainB ],
PID = spawn(fun() ->
train(1,length(Trains)) end),
[ PID ! {self(),TrainData,TotalDist} || TrainData <- Trains],
receive
{_From, Mesg} ->
error_logger:info_msg("~n Mesg ~p ~n",[Mesg])
after 10500 ->
refresh
end.
所以,我创建了两个名为trainA、trainB的进程。我想将这些进程增加 5,直到达到 100。我做了不同的进程,使每个火车(进程)并行增加其位置。但我很惊讶地顺序得到输出,即进程 trainA 结束,然后进程 trainB 开始。但我想同时增加自己。 我想运行这样的进程,
trainA 10 trainB 0
trainA 15 trainB 5
....
trainA 100 trainB 100
但我得到了
trainA 0
....
trainA 90
trainA 95
trainA 100
trainA ends
trainB 0
trainB 5
trainB 10
.....
trainB 100
如何使进程并行/同时运行?希望你能收到我的问题。请帮我。
startTrains() ->
TotalDist = 100,
Trains = [trainA,trainB ],
PID = spawn(fun() ->
train(1,length(Trains)) end),
[ PID ! {self(),TrainData,TotalDist} || TrainData <- Trains],
receive
{_From, Mesg} ->
error_logger:info_msg("~n Mesg ~p ~n",[Mesg])
after 10500 ->
refresh
end.
so, I created Two Processes named trainA, trainB. I want to increment these process by 5 till it gets 100. I made different processes to make each of the train (process) increments its position parallely. But I was surprised to get the output sequentially i.e process trainA ends then process trainB starts. But I want to increment themselves at simultaneously.
I want to run processes like this
trainA 10 trainB 0
trainA 15 trainB 5
....
trainA 100 trainB 100
but I m getting
trainA 0
....
trainA 90
trainA 95
trainA 100
trainA ends
trainB 0
trainB 5
trainB 10
.....
trainB 100
How to make the processes run parallel/simultaneously? Hope you get my Q's. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仅生成一个由函数
train/2
初始化的进程。您提供的代码不完整,所以我只能猜测,但我认为您的代码是错误的,因为您只有一个火车流程。寻求灵感:但如果我认真思考的话,我应该考虑 gen_fsm 和 OTP 原则。但在当前阶段,请继续使用 erlang 原语,以便首先获得更好的感觉。
You spawn only one process initialized by function
train/2
. Your presented code is incomplete so I can only guess but I think your code is wrong because you have only one train process. For inspiration:But if I would think it seriously I should look to
gen_fsm
and OTP principles. But in your current stage keep play with erlang primitives to take better feeling first.