2

假设我有一个 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
%%--------------------------------------------------------------------
4

2 回答 2

9

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

我建议你 spawn_link 一个辅助进程来执行计算,哪些消息将结果传递回 gen_server。将对此侧进程的引用保持在 gen_server 的状态。在生成的同时,生成一个timer将作为边计算长度限制的。如果您的 handle_info 在边计算运行时收到超时,请终止边进程并退出。如果您首先收到来自边计算的结果,请终止计时器。

于 2013-09-06T21:53:03.897 回答
3

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

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

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

于 2013-09-08T04:24:18.230 回答