简体   繁体   English

Erlang:从另一个模块产生一个进程到一个函数

[英]Erlang: spawn a process to a function from another module

I started this week with this programming language and I'm trying to do a very simple web server for a subject on my university. 我本周开始使用这种编程语言,并且正在尝试为我大学中的一门学科提供一个非常简单的Web服务器。 I have a question when I spawn a process. 生成进程时有一个问题。 I need to spawn N process, so I create a function inside my module dispatcher.erl: 我需要产生N个进程,因此我在模块dispatcher.erl中创建了一个函数:

create_workers(NWorkers, Servers, Manager, Dispatcher_Pid) ->
case NWorkers of
    0 -> Servers;
    _ when NWorkers > 0 ->
        Process_Pid = spawn(server, start, [Manager]),
        create_workers((NWorkers - 1), lists:append(Servers, [{server, Process_Pid}]), Manager, Dispatcher_Pid)
end.

The function I'm trying to call is in another module (server.erl), which contains this code: 我尝试调用的函数在另一个模块(server.erl)中,该模块包含以下代码:

start(Manager, Dispatcher_Pid) ->
receive
    {request, Url, Id, WWW_Root} ->
        case file:read_file((WWW_Root ++ Url)) of
            {ok, Msg} ->
                conn_manager:reply(Manager, {ok, binary:bin_to_list(Msg)}, Id);
            {error, _} ->
                conn_manager:reply(Manager, not_found, Id)
        end
end,
Dispatcher_Pid ! {done, self()},
start(Manager, Dispatcher_Pid).

So, I'm trying to spawn a process from the dispatcher.erl module to a function on server.erl module, but I get this error on every spawn: 因此,我试图将一个进程从dispatcher.erl模块生成为server.erl模块上的一个函数,但是每次生成时都会出现此错误:

=ERROR REPORT==== 24-Mar-2015::01:42:43 === 
Error in process <0.165.0> with exit value: {undef,[{server,start,[<0.159.0>],[]}]}

I don't know what is happening here, because I think I'm calling the spawn function as the Erlang documentation says, can I get some help? 我不知道这里发生了什么,因为我想按照Erlang文档中的说明调用spawn函数,我能得到一些帮助吗?

Thanks for your time! 谢谢你的时间!

Okay, I resolved it from myself. 好吧,我自己解决了。 When I spawn a new process, I was passing it less arguments than the arity of the function I'm trying to spawn. 当我产生一个新进程时,我传递给它的参数少于我试图产生的函数的稀疏性。

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

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