在Erlang中实现取消

时间:2013-09-06 21:16:49

标签: design-patterns erlang gen-server

假设我有一个gen_server执行一些长时间运行的任务。什么是取消任务的最好和最干净的方法?

-module(example_srv).

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
     terminate/2, code_change/3]).

-record(state, {}).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
    {ok, #state{}}.

%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(long_run, _From, State) ->
    other_module:long_running_function(),
    Reply = ok,
    {reply, Reply, State};        
handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%%                                       {noreply, State, Timeout} |
%%                                       {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
    ok.

%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------

2 个答案:

答案 0 :(得分:9)

你的长期计算是同步的 - gen_server循环的一部分 - 你将无法中断它。 同步计算完成后,将收到您发送给gen_server进程的任何消息

我建议你使用spawn_link一个副进程来执行计算,消息将结果传递给gen_server。在gen_server的状态中保持对此侧进程的引用。在产卵的同时,产生一个timer,它将作为边计算长度的限制。如果你的handle_info在边计算运行时收到超时,则终止副流程并挽救。如果您首先从侧面计算中收到结果,请终止计时器。

答案 1 :(得分:3)

我最近遇到了中间人模式。

这个想法是你的gen-server产生一个负责运行/监控进程的中间进程/ gen_server,然后可以使用这个中间进程与长时间运行的进程进行交互。

如果您想要取消长时间运行的进程,您只需向中间人发送一条消息来杀死该工作人员 - (中间人的行为可能会在某种程度上由gen_server处理)