简体   繁体   English

如何在Erlang中创建一系列已注册的进程名称?

[英]How can I create a sequence of registered process names in Erlang?

At the moment I am using the following to create and register processes individually: 目前我正在使用以下内容单独创建和注册流程:

register(name, spawn(fun() -> myfun())).

I would like to create a list of N registered Pids with names as follows: 我想创建一个N个已注册Pid的列表,其名称如下:

pid1
pid2
pid3
.
.
.
pidN

Can anyone recommend a way of doing this? 任何人都可以推荐一种方法吗?

如果要为同一个函数生成多个进程,可以使用列表list_to_atom/1并使用字符串和list_to_atom/1串联:

[register(list_to_atom("pid" ++ integer_to_list(X)), spawn(fun() -> myFun() end)) || X <- lists:seq(1,10)].

You might use following: 您可以使用以下内容:

if you want to execute same function in multiply processes and register each process under different names: 如果要在乘法进程中执行相同的函数并以不同的名称注册每个进程:

lists:foreach( 
   fun( Name ) -> 
      register( Name, spawn( fun() -> myfun() end ) ) 
   end, 
   [ pid1, pid2, pid3 ] ).

or if you want to execute different functions in different processes: 或者如果要在不同的进程中执行不同的功能:

lists:foreach( 
   fun( { Name, Func } ) -> 
      register( Name, spawn( fun() -> Func() end ) ) 
   end, 
   [ { pid1, f1 }, { pid2, f2 }, { pid3, f3 } ] ).

( f1, f2, f3 - are functions ) (f1,f2,f3 - 是函数)

and finally, if you want to execute same function in N processes, you might do next: 最后,如果你想在N个进程中执行相同的功能,你可能会做下一个:

N = 20, %number of processes
lists:map( 
   fun( Num ) -> 
      register( 
         list_to_atom( "pid" ++ integer_to_list( Num ) ), 
         spawn( fun() -> myfun() end ) ) 
   end, 
   lists:seq( 1, N ) ).

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

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