系统相关
首页 > 系统相关> > 从php在服务器上运行prolog(或使其成为守护进程)

从php在服务器上运行prolog(或使其成为守护进程)

作者:互联网

我正在学习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

现在,我还有序言代码,它可以借助以下帮助在本地主机上启动服务器:

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库(或者我只是不知道如何正确调用它).

我不是一个好的sysadmin,所以我确实需要任何建议如何在php服务器上的表单上运行脚本.

或者,如果我可以修改它,使其仅使用SWIPL就可以作为服务器上的守护程序使用,那么这也可能对我有用.

谢谢.

解决方法:

我认为您遇到的问题是服务器生成后立即调用了halt / 0,因此它甚至没有机会监听单个请求.

以我的经验,Web服务器的最佳方法是将SWI作为Unix守护程序运行,这也是您的建议.请参阅library(http/http_unix_daemon)的文档.

使用此库时,您可以简单地运行服务器(例如):

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

并且它将持续监听请求并为客户提供服务.

请注意,如果您使用库(http / http_unix_daemon),则甚至不需要诸如server / 1之类的辅助谓词.所有这些都是隐式处理的.

在开发过程中,建议您在启动服务器时使用–interactive命令行标志,以便可以在顶层与服务器进行交互.

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

标签:swi-prolog,prolog,server,daemon,php
来源: https://codeday.me/bug/20191118/2030322.html