Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Process 延迟赢得';行不通_Process_Erlang_Delay - Fatal编程技术网

Process 延迟赢得';行不通

Process 延迟赢得';行不通,process,erlang,delay,Process,Erlang,Delay,我正在制作一个活动日历,作为学校的一项任务,我对这一点还不熟悉。 问题是,当我在循环的接收部分进行延迟时,我的表消失了。 我在网上和我的代码中查找错误已经两天了 事件是一个元组=>{Time(类似于now()的元组,make_ref()},NotifyPid,Load} getTime通常返回一个整数 问题可能是您的start/0函数生成的进程崩溃。当进程崩溃时,它拥有的所有ETS表都会被捕获。请尝试使用spawn\u monitor然后使用shell的flush()获取传入消息的命令。该命令可

我正在制作一个活动日历,作为学校的一项任务,我对这一点还不熟悉。 问题是,当我在循环的接收部分进行延迟时,我的表消失了。 我在网上和我的代码中查找错误已经两天了

事件是一个元组=>{Time(类似于now()的元组,make_ref()},NotifyPid,Load} getTime通常返回一个整数


问题可能是您的
start/0
函数生成的进程崩溃。当进程崩溃时,它拥有的所有ETS表都会被捕获。请尝试使用
spawn\u monitor
然后使用shell的flush()获取传入消息的命令。该命令可能会失效。另一种方法是使用
proc_lib
模块中的工具,然后使用
erl-boot start\u sasl
为进程生成并运行一些基本的崩溃错误报告

“裸”的
spawn(…)
通常是危险的,因为如果生成的进程崩溃,您将什么都学不到。至少使用
spawn\u链接
spawn\u监视器

我发现了我的问题:
我正在测试我的代码,但我没有一个Pid来测试,所以我使用了
whereis('event manager')
。相反,我必须使用
self()

提供在问题出现之前调用的命令以及该问题的症状(Erlang shell输出)。Thx
-module(calender).
-export([start/0, start_new/0, post/1, postincr/1, gettime/0]).
-export ([kalender/0, getTime/1, increment/1, makeTime/1]). % internal use only

%% @doc Starts the program
start() ->
    case whereis('event manager') =:= undefined of
        true ->
            register('event manager', spawn(calender, kalender, [])),
            {ok, 'event manager'};
        false ->
            {event_not_made}
    end.

%% @doc Starts a new program even program already exist but kills it first
start_new() ->
    case whereis('event manager') =:= undefined orelse unregister('event manager') of
        true ->
            ets:delete(calend),
            register('event manager', spawn(calender, kalender, [])),
            {ok, 'event manager'};
        false ->
            {ok, event_not_made}
    end.

% Puts Events into sorted table
% Time is an integer value in milliseconds  
post(Event) ->
    'event manager'!{post, Event},
    {ok, gettime()}.

%% @doc Puts Events into sorted table
%% Increment is an integer value which will be added to the present time
%% The increment value of time is in milliseconds
%% @end
postincr(Event) ->
    'event manager'!{postincr, Event},
    {ok, gettime()}.

%% @doc Gives the difference in time between present and time at start
gettime() ->
    'event manager'!{gettime, self()},
    receive
        T -> T
    end.

%% @private Calculates the difference of time between the present time and Event time
getTime(Time) ->
    NowTime = now(),
    timer:now_diff(Time, NowTime)div 1000.

%% @private Adds the incremental time of postincr to the present time
increment(Incr) ->
    {X, Y, Z} = now(),
    X1 = X * 1000000000000,
    Y1 = Y * 1000000,
    Incr1 = X1 + Y1 + Z + (Incr * 1000),
    makeTime(Incr1).

%% @private Changes integer to tuple of 3 values
makeTime(Time) ->
    X = Time div 1000000000000,
    Y = (Time rem 1000000000000) div 1000000,
    Z = Time rem 1000000,
    {X, Y, Z}.

%% @private Makes the sorted table, starts the loop
kalender() ->
    Cal = {ets:new(calend, [ordered_set, named_table, {keypos, 1}, public]), now()},
    loop(Cal).

%% @private Loops through the table and checks for received messages
loop(Cal) ->
    io:format("Loop start ~n"),
    {Calen, TimeAtStart} = Cal,

    io:format("Before case ~n"),
    case ets:first(Calen)  of
        '$end_of_table' ->
            io:format("end of table ~n"),
            {end_of_table};
        {Time, Ref} ->
            io:format("Before calculation event ~n"),
            Ms = getTime(Time),
            io:format("After getTime ~n"),
            if 
                Ms =< 0 ->
                    io:format("Ms =< 0 ~n"),
                    [Event | _Rest] = ets:lookup(Calen, {Time, Ref}),
                    io:format("~p~n", [Event]),
                    {{_Time1, _Ref1}, NotifyPid, _Load} = Event,
                    io:format("~p~n", [NotifyPid]),
                    NotifyPid!Event,
                    io:format("After event send ~n"),
                    ets:delete(Calen, {Time, Ref}),
                    io:format("After Ms =< 0 ~n");
                Ms > 0 ->
                    io:format("Event not done ~n"),
                    {event_not_yet_done}
            end,
            io:format("After calculation event ~n")
    end,
io:format("Before Delay ~n"),
        % Gets the delay time
        Delay = case ets:first(Calen) of
            '$end_of_table' ->
                io:format("Delay infinity ~n"),
                infinity;
            {DelayTime, _DelayRef} ->
                io:format("~p~n", [DelayTime]), => the DelayTime has for example a value of {9283,823031,155000}

                Dl = getTime(DelayTime),
                case Dl > 0 of
                    true ->
                        Dl,
                        io:format("~p~n", [Dl]); => this io:format gives me on the screen a calculated value example: 7899995274337

                    false ->
                        0,
                        io:format("0 ~n")
                end,
                io:format("Delay time~n")
        end,

        io:format("Before receive ~n"),
        receive
            {post, PostEvent} -> 
                io:format("In post ~n"),
                {PostTimeI, Np, Ld} = PostEvent,
                PostRef = make_ref(),
                PostTimeT = makeTime((PostTimeI * 1000)),
                io:format("After making the tuples ~n"),
                io:format("~p   ~p  ~p  ~p  ~p~n", [PostTimeI, PostRef, PostTimeT, Np, Ld]),
                ets:insert(Calen, {{PostTimeT, PostRef}, Np, Ld}),
                io:format("After insert post ~p~n", [whereis('event manager')]);
            {postincr, PostIncrEvent} ->
                {Incr, Np, Ld} = PostIncrEvent,
                PostIncrRef = make_ref(),
                PostIncrTime = increment(Incr),
                ets:insert(Calen, {{PostIncrTime, PostIncrRef}, Np, Ld});
            {gettime, From} ->
                From!getTime(TimeAtStart)
            after
                Delay ->
                    io:format("Delaying ~n"),
                    {ok}
        end,
        io:format("After receive ~n"),
        loop(Cal).