运行 来自 php 的服务器上的序言(或使其成为守护进程)

running prolog on a server from php (or make it daemon)

我正在学习 Prolog,我确实想了解它如何用于现实世界的 Web 应用程序。一切都在本地主机上完美运行,但我在让我的创作上线时遇到了一些麻烦。

要在服务器上启动它,我遵循了本教程:http://www.j-paine.org/dobbs/prolog_from_php.html

通过对 php 进行一些更改,我让它上线了。 我的 php 代码:

<html>
<head>
<title>Calling SWI-Prolog from PHP (short)</title>
</head>
<body>
<? 
  $cmd = "swipl -f /path/to/myfile.pl -g test,halt -t 'halt(1)'";

  system( $cmd );
  echo "\n";

  $output = exec( $cmd );
  echo $output;
  echo "\n";
?> 
</body>
</html>

一切正常,结果如下:http://37.139.24.44/index.php

现在,我还有 prolog 代码,它在我的本地主机上启动服务器,借助于:

server(Port) :-
        http_server(http_dispatch, [port(Port)]).

示例代码为:

:- module(upload, [ run/0]).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- use_module(library(http/http_mime_plugin)).
:- use_module(library(http/http_client)).
:- use_module(library(http/html_write)).
:- use_module(library(lists)).

:- http_handler(root(.),    upload_form, []).
:- http_handler(root(upload),   upload,      []).

run :-
    http_server(http_dispatch, [port(8080)]).

upload_form(_Request) :-
    reply_html_page(
        title('Upload a file'),
        [ h1('Upload a file'),
          form([ method('POST'),
             action(location_by_id(upload)),
             enctype('multipart/form-data')
           ],
           table([],
             [ tr([td(input([type(file), name(file)]))]),
               tr([td(align(right),
                  input([type(submit), value('Upload!')]))])
             ]))
        ]).

upload(Request) :-
    (   memberchk(method(post), Request),
        http_read_data(Request, Parts, [form_data(mime)]),
        member(mime(Attributes, Data, []), Parts),
        memberchk(name(file), Attributes),
        memberchk(filename(Target), Attributes)
    ->  % process file here; this demo just prints the info gathered
        atom_length(Data, Len),
        format('Content-type: text/plain~n~n'),
        format('Need to store ~D characters into file \'~w\'~n',
           [ Len, Target ]),
        name(Data,Lis),
        write(Lis)
    ;   throw(http_reply(bad_request(bad_file_upload)))
    ).

:- multifile prolog:message//1.

prolog:message(bad_file_upload) -->
    [ 'A file upload must be submitted as multipart/form-data using', nl,
      'name=file and providing a file-name'
    ].

我想从 php 调用这个,在实时服务器上不断地 运行 它,而不需要终端中的任何命令。

我已经尝试将 php 更改为

<? 
$cmd = "swipl -f /path/to/myfile.pl -g run,halt -t 'halt(1)'";
system( $cmd );
$output = exec( $cmd );
echo $output;
echo "\n";
?>

但它只给我一个空白屏幕。 我想这可能是因为我正在尝试 运行 已经运行的服务器上的服务器?

而且如果我尝试从 php 调用其他谓词,它不能与所需的 http 库一起使用(或者我只是不知道如何正确调用它)。

我不是很好的系统管理员,所以我确实需要任何建议如何 运行 使用来自 php.

的服务器上的表单的脚本

或者,如果我可以对其进行调整,使其作为服务器上的守护程序运行,仅使用 SWIPL,那也可能对我有用。

谢谢。

我认为你的问题是你在服务器生成后立即调用 halt/0,所以它甚至没有机会听一个请求。

根据我的经验,Web 服务器的最佳方法是 运行 SWI 作为 Unix daemon,这也是您的建议。请参阅 library(http/http_unix_daemon).

的文档

当你使用这个库时,你可以简单地运行服务器作为(例如):

$ swipl server.pl --user=www --pidfile=/var/run/http.pid

它会不断地听取请求并为客户提供服务。

请注意,如果您使用 library(http/http_unix_daemon),您甚至不需要像 server/1 这样的辅助谓词。所有这些都是隐式处理的。

在开发过程中,我建议您在启动服务器时使用--interactive命令行标志,这样您也可以在顶层与服务器进行交互。

完成后,您可以在系统启动时轻松地运行服务器。