每次迭代时的 Erlang 动态变量名称
Erlang dynamic variable names on each iteration
程序正在运行。最简单的解决方案是最好的。注册的进程不起作用。我实际上在从 start/6 的基本情况调用 await/2 时遇到了一些麻烦。原来我使用的是修改后的列表 T_T.
%variables
T = 10000000,
P = 4,
Tpart = 2500000,
Width = 1.0 / T,
Pi = pi(T, P, [], 1), calls pi/4 and gets a list of pid
start(T, P, Tpart, Width, Pi, 1). %calls start with the Pi variable which should contain 4 Pids
% create a list of P Pids and return it
pi(_, P, List, Count) when Count > P -> List;
pi(T, P, List, Count) ->
Pid = spawn(pi, child, []),
Count2 = Count + 1,
pi(T,P, [Pid|List], Count2).
%start iterates through a list of Pids and sends a work order to each process
start(_, P, _, _, _, StaticList, Count) when Count > P -> await(StaticList, 0.0);
start(T, P, Tpart, Width, [Head|Tail], StaticList, Count) ->
Head ! {work, self(), P, Count, Tpart, Width},
Count2 = Count + 1,
start(T,P,Tpart,Width,Tail, StaticList, Count2).
% Collects the partial sums from child processes. Print final output
await([], Final) -> io:format(" Final Sum: ~.8f \n", [(Final * 4.0)]);
await([Pid | Rest], Final) ->
receive
{done, Pid, Sum} ->
Partial = Final + Sum,
await(Rest, Partial)
end.
您不能直接命名 Pid。但是,您可以使用进程名称,如果您不小心,可以使用 register it using register/2
. You should be careful, though, because the registered process name needs to be an atom and you could potentially fill up the atom table。
%Call to function
Pi = pi(T,P, [], 1, getName(1)), %list of Pids
% This is the main function that should return a list of Pid's with different names.
pi(T, P, List, Count) when Count >= P -> List;
pi(T, P, List, Count) ->
Pid = spawn(pi, child, []), % I want to have name be: Pidx = ...
Count2 = Count + 1,
Name = getName(Count2),
register(Name,Pid),
pi(T,P, [Name|List], Count2).
%Helper method that returns an atom. I want to use this as a variable name in the main function.
getName(Count) ->
X = "Pid",
Y = integer_to_list(Count),
Name = X ++ Y,
list_to_atom(Name).
如果您需要 Pid 列表,为什么要命名它们?你可以这样做:
%Call to function
Pi = pi(T,P, [], 1, getName(1)), %list of Pids
% This is the main function that should return a list of Pid's with different names.
pi(T, P, List, Count) when Count >= P -> List;
pi(T, P, List, Count) ->
Pid = spawn(pi, child, []), % I want to have name be: Pidx = ...
Count2 = Count + 1,
pi(T,P, [Pid|List], Count2).
这将 return 一个 Pid 列表,而不需要其他辅助函数。
程序正在运行。最简单的解决方案是最好的。注册的进程不起作用。我实际上在从 start/6 的基本情况调用 await/2 时遇到了一些麻烦。原来我使用的是修改后的列表 T_T.
%variables
T = 10000000,
P = 4,
Tpart = 2500000,
Width = 1.0 / T,
Pi = pi(T, P, [], 1), calls pi/4 and gets a list of pid
start(T, P, Tpart, Width, Pi, 1). %calls start with the Pi variable which should contain 4 Pids
% create a list of P Pids and return it
pi(_, P, List, Count) when Count > P -> List;
pi(T, P, List, Count) ->
Pid = spawn(pi, child, []),
Count2 = Count + 1,
pi(T,P, [Pid|List], Count2).
%start iterates through a list of Pids and sends a work order to each process
start(_, P, _, _, _, StaticList, Count) when Count > P -> await(StaticList, 0.0);
start(T, P, Tpart, Width, [Head|Tail], StaticList, Count) ->
Head ! {work, self(), P, Count, Tpart, Width},
Count2 = Count + 1,
start(T,P,Tpart,Width,Tail, StaticList, Count2).
% Collects the partial sums from child processes. Print final output
await([], Final) -> io:format(" Final Sum: ~.8f \n", [(Final * 4.0)]);
await([Pid | Rest], Final) ->
receive
{done, Pid, Sum} ->
Partial = Final + Sum,
await(Rest, Partial)
end.
您不能直接命名 Pid。但是,您可以使用进程名称,如果您不小心,可以使用 register it using register/2
. You should be careful, though, because the registered process name needs to be an atom and you could potentially fill up the atom table。
%Call to function
Pi = pi(T,P, [], 1, getName(1)), %list of Pids
% This is the main function that should return a list of Pid's with different names.
pi(T, P, List, Count) when Count >= P -> List;
pi(T, P, List, Count) ->
Pid = spawn(pi, child, []), % I want to have name be: Pidx = ...
Count2 = Count + 1,
Name = getName(Count2),
register(Name,Pid),
pi(T,P, [Name|List], Count2).
%Helper method that returns an atom. I want to use this as a variable name in the main function.
getName(Count) ->
X = "Pid",
Y = integer_to_list(Count),
Name = X ++ Y,
list_to_atom(Name).
如果您需要 Pid 列表,为什么要命名它们?你可以这样做:
%Call to function
Pi = pi(T,P, [], 1, getName(1)), %list of Pids
% This is the main function that should return a list of Pid's with different names.
pi(T, P, List, Count) when Count >= P -> List;
pi(T, P, List, Count) ->
Pid = spawn(pi, child, []), % I want to have name be: Pidx = ...
Count2 = Count + 1,
pi(T,P, [Pid|List], Count2).
这将 return 一个 Pid 列表,而不需要其他辅助函数。