热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何确定进程父级的PID-HowDoYouDetermineThePIDoftheParentofaProcess

Ihaveaprocessinerlangthatissupposedtodosomethingimmediatelyafterspawn,thensendther

I have a process in erlang that is supposed to do something immediately after spawn, then send the result back to the parent when it is finished. How do I figure out the PID of the process that spawned it?

我在erlang中有一个进程,它应该在spawn之后立即执行某些操作,然后在结束时将结果发送回父进程。我如何计算产生它的过程的PID?

4 个解决方案

#1


14  

You should pass self() to the child as one of the arguments to the entry function.

您应该将self()作为entry函数的参数之一传递给子节点。

spawn_link(?MODULE, child, [self()]).

#2


4  

@Eridius' answer is the preferred way to do it. Requiring a process to register a name may have unintended side-effects such as increasing the visibility of the process not to mention the hassle of coming up with unique names when you have lots of processes.

@Eridius的回答是首选的方法。要求进程注册名称可能会产生意想不到的副作用,例如提高进程的可见性,更不用说在有大量进程时提出独特名称的麻烦。

#3


3  

The best way is definitely to pass it as an argument to the function called to start the child process. If you are spawning funs, which generally is a Good Thing to do, be careful of doing:

最好的方法是将它作为参数传递给被调用以启动子进程的函数。如果你正在产生乐趣,这通常是一件好事,小心做:

spawn_link(fun () -> child(self()) end)

which will NOT do as you intended. (Hint: when is self() called)

这不会像你想象的那样做。 (提示:何时调用self())

Generally you should avoid registering a process, i.e. giving it a global name, unless you really want it to be globally known. Spawning a fun means that you don't have to export the spawned function as you should generally avoid exporting functions that aren't meant to be called from other modules.

一般来说,你应该避免注册一个进程,即给它一个全局名称,除非你真的希望它是全局知道的。产生乐趣意味着您不必导出衍生函数,因为您通常应该避免导出不打算从其他模块调用的函数。

#4


0  

You can use the BIF register to give the spawning / parent process a name (an atom) then refer back to the registered name from other processes.

您可以使用BIF寄存器为产卵/父进程提供名称(原子),然后返回其他进程的注册名称。

FUNC() ->

%% Do something
%% Then send message to parent
parent ! MESSAGE.

%%做一些%%然后向父母发送消息!信息。

...

register(parent, self()),
spawn(MODULE, FUNC, [ARGS]).

寄存器(parent,self()),spawn(MODULE,FUNC,[ARGS])。

See Getting Started With Erlang §3.3 and The Erlang Reference Manual §10.3.

请参阅Erlang§3.3入门和Erlang参考手册§10.3。


推荐阅读
author-avatar
手机用户2502876011
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有