SWI-Prolog / packages-pengines

Pengines: Prolog engines
12 stars 13 forks source link

Possible bug... or just needs documentation? #16

Open torbjornlager opened 10 years ago

torbjornlager commented 10 years ago

Here, test1/0 and test2/0 should ideally behave in exactly the same way, but do not:

:- use_module(library(pengines)).

test1 :-
    pengine_create([
        src_text("
            run :-
               sleep(5),
               pengine_output(ping).
        ")]),
    pengine_event_loop(handler1, []).

handler1(create(ID, Data)) :- !,
    writeln(create(ID, Data)),
    pengine_ask(ID, run, []).
handler1(Event) :-
    writeln(Event).

test2 :-
    pengine_create([
        ask(run), % Note the ask option!
        src_text("
            run :-
               sleep(5),
               pengine_output(ping).
        ")]),
    pengine_event_loop(handler2, []).

handler2(Event) :-
    writeln(Event).

?- test1.
% After 0 seconds
create(d70f3973-87e0-448c-9080-4580a02b8508,[slave_limit(3)])
% After 5 seconds
output(d70f3973-87e0-448c-9080-4580a02b8508,ping)
success(d70f3973-87e0-448c-9080-4580a02b8508,[run],false)
destroy(d70f3973-87e0-448c-9080-4580a02b8508)
true.

?- test2.
% After 5 seconds
create(384e4387-22a8-471a-915c-3ceace71b5f8,[slave_limit(3)])
output(384e4387-22a8-471a-915c-3ceace71b5f8,ping)
success(384e4387-22a8-471a-915c-3ceace71b5f8,[run],false)
destroy(384e4387-22a8-471a-915c-3ceace71b5f8)
true.

?- 

The problem is that the pengine in test2/0 is waiting to wrap the first answer in the create event, but the first answer doesn't come until after 5 seconds. Now, suppose we had a blocking call instead of sleep/1 - that wouldn't work at all! (Fortunately, pengine_input/2 isn't blocking in a harmful way since it first prompts and it is the prompt that gets wrapped.)

Is this even fixable? If not, the fact that the use of the ask option and the use of pengine_ask/3 aren't always equivalent should perhaps be documented.