许多短期生活过程与全球流程柜台 [英] Many short-living processes with global process counter

查看:148
本文介绍了许多短期生活过程与全球流程柜台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要启动许多短暂的进程,由某种 spawner 进程启动,这些进程自己做一些工作,保存结果(或委托保存到一些保存过程),然后退出。



同时也应该有一些全局限制并行运行的进程数。而且我想有一些队列,因为当达到限制时,那么 spawner 可以继续产生新的进程,而我猜应该挂起来等待房间运行。 / p>

可以在erlang中轻松实现该方案吗?有工作人员池可以获得一些速度或只是每次都产生新的过程是合理的吗?可以在这里提供简单的帮助吗?

解决方案

如已经指出的那样,您可以轻松地生成过程,您的产卵过程可以使用监视器/ 2和一个计数器来限制和维护你的工作流程池。您的产卵过程将产生一组初始过程,监控所产生的蚜虫(即监视(过程,Pid))并酌情递减计数器。然后,它将进入一个等待{'DOWN',MonitorRef,Type,Object,Info}的消息,指示工作人员已经终止。正如...

 循环(MaxProcesses,LiveProcesses) - > 
接收
{'DOWN',_Ref,process,_Pid,_Info} - >
case当$ N =<
N时的LiveProcesses MaxProcesses - >
spawn_another()%并监视新的pid
循环(MaxProcess,LiveProcesses);
N当N> MaxProcess - >
循环(MaxProcesses,LiveProcesses -1)
end
end
after%可选,如果您希望spawner潜在地执行其他操作
Timeout->
做更多的工作
循环(MaxProcesses,LiveProcesses)
end
end。


I need to start many short-lived processes to be started by some kind of spawner process, those processes by itselves do some work, save result (or delegate saving to some saver process) and then exit.

And there also should be some global limit of the number of parallel running processes. And i suppose some kind of queue, because when limit is reached, then spawner could continue to spawn new processes while they i guess should suspend and wait for the room to run.

Could that scheme be implemented easily in erlang ? Does it make sense to have pool of workers to gain some speed or just spawn new process every time is okay ? Could supervisor simple_one_for_one help here ?

解决方案

You can spawn process easily, as already pointed out, and your spawning process could use monitor/2 and a counter to limit and maintain your worker process pool. Your spawning process would spawn an initial set of processes, Monitoring the resulting Pids (i.e., monitor(process, Pid)) and decrementing a counter as appropriate. It would then go into a receive waiting on a {'DOWN', MonitorRef, Type, Object, Info} message indicating that a worker had terminated. As in...

loop(MaxProcesses, LiveProcesses) ->
    receive
       {'DOWN', _Ref, process, _Pid, _Info} ->
           case LiveProcesses of
               N when N =< MaxProcesses -> 
                   spawn_another() %and monitor new pid
                   loop(MaxProcess, LiveProcesses);
               N when N > MaxProcess -> 
                   loop(MaxProcesses, LiveProcesses -1)
           end
        end
    after  %optional if you want the spawner to potentially do something else
        Timeout->
           do more work
           loop(MaxProcesses, LiveProcesses)
        end
    end.

这篇关于许多短期生活过程与全球流程柜台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆