我正在尝试修改我的网站,以允许用户调用我正在处理的特定程序。当我“正常”(即从REPL)在Prolog中使用process_create/3
时,一切正常。但是,当我尝试从HTTP处理程序中调用它时,所有内容都会返回一些非零的退出代码(ls
给我2
,pwd
和echo
给我1
等)。下面是一个完整的简单程序,演示了这一点:
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_unix_daemon)).
:- use_module(library(process)).
:- initialization(start_server, main).
:- http_handler(root('run'), try_run, []).
start_server :-
try_run(_),
http_daemon([port(8080), user(root)]).
try_run(_Request) :-
format('Content-type: text/plain~n~n', []),
setup_call_cleanup(
process_create(path(echo), ['hello world'], [process(PID)]),
true,
process_wait(PID, Code)),
format('exited with: ~w~n', [Code]).
显然以root
运行不是理想的,但我想确保没有权限错误或任何错误。
上面,我们可以看到,如果只是运行启动服务器,则对try_run/1
的调用在start_server/0
内部有效。
roei@roei-main:~$ swipl --version
SWI-Prolog version 8.0.3 for x86_64-linux
roei@roei-main:~/Prolog/website$ sudo swipl t.pl
Content-type: text/plain
hello world
exited with: exit(0)
但是,如果我尝试发出请求:
roei@roei-main:~/Prolog/website$ curl -X POST localhost:8080/run
exited with: exit(1)
我正在运行Ubuntu 18.04。
似乎问题是,无论出于何种原因,无论什么process_create/3
用作默认输出流,都无法写入。但是,如果我将其更改为:
try_run(_Request) :-
format('Content-type: text/plain~n~n', []),
setup_call_cleanup(
process_create(path(echo), ['hello world'], [process(PID), stdout(pipe(Stream))]),
true,
process_wait(PID, Code)),
current_output(Out),
copy_stream_data(Stream, Out),
format('exited with: ~w~n', [Code]).
即,手动设置输出流,现在可以使用。