« Return to Thread: Returning JSON output from a handler thread still running?

Re: Returning JSON output from a handler thread still running?

by Jan Wielemaker-3 :: Rate this Message:

Reply to Author | View in Thread

On Thursday 18 June 2009 14:57:27 Torbjörn Lager wrote:

> Hello,
>
> Please have a look at the following code:
> :- use_module(library('http/thread_httpd')).
> :- use_module(library('http/http_dispatch')).
> :- use_module(library('http/json')).
> :- use_module(library('http/http_json')).
> :
> :- http_handler('/doit', doit, [spawn([alias(test)])]).
>
> doit(_Requestl) :-
>     reply_json(json([a=result])),
>     thread_get_message(Event),
>     process(Event).
>
> :- http_server(http_dispatch, [port(5000)]).
>
> The call to thread_get_message/1 blocks (as it should), which seems to
> imply that no JSON is returned to the browser (until I kill the
> server). Does it have to be that way (I don't see why)? Is there any
> way I can send JSON to the client and still keep the thread on the
> server running?

Not easily. The whole infrastructure is designed to deal with finishing
the reply on completion of the handler. What you can do is enable
session management (you need that anyway) and create a thread that is
associated to the session. That is where you have the state-full
computation and you send messages back and forth between the HTTP
handlers and the session thread to communicate queries and results.

Would be nice to have a demo using this scenario in the HTTP server
examples directory ...

More in general, I'm looking for good examples for this (and other)
packages.  If you have things to share or are willing to write examples,
please share them.

        Cheers --- Jan

> (What I'm trying to find here is a way to ask for one solution at the
> time for a Prolog goal entered in a web interface. I have written code
> that should have worked, had only the above worked... Maybe there are
> other ideas for how to accomplish what I want to do? I've looked at
> Uwe Lestas N-Queens demo, but it striked me as too complicated, and I
> really would like to work against the SWI webserver...)
>
> Thanks in advance!
>
> Best regards,
> Torbjörn



_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

 « Return to Thread: Returning JSON output from a handler thread still running?