1

There is module to call from Python (pyswip) =to=> Prolog, but is there a way to call the other way around from Prolog =to=> Python ?

I need it so I can call Spacy-NLP module from SWI-Prolog


couldn't find if SWI supports ZeroMQ.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
sten
  • 7,028
  • 9
  • 41
  • 63

1 Answers1

2

This is the sample python code in a file "c:\hello.py" that extracts the argumentq on the command line (optional). It's results are echoed to the standard output stream.

import sys
def hello():
    return 'hello world: ' + ','.join(sys.argv[1:])
if __name__ == '__main__':
    print(hello())

Here is the prolog program in file "c:\hello.pl" invoking the above python code as a process:

do :-
    process_create(path('python3.7'),
                   ['c:/hello.py', foo, bar],
                   [stdout(pipe(In))]), %output stream named In
    read_string(In, Len, X), %In=input stream to read_string/3
    write(X), nl, halt.

To activate this prolog/python combo and writting the results to the output stream

$ swipl -g do c:\hello.pl
hello world: foo,bar

Does this do what you wanted?

peter.cyc
  • 1,763
  • 1
  • 12
  • 19
  • i see what you did... wouldn't this be too slow if I have hundreds and thousands of calls ... can this be used for multiple calls w/o closing the python process ? – sten Jan 17 '21 at 00:40
  • You asked for one way to communicate between two systems. this is one solution and you understood it. Your next question involves designing an efficient solution architecture. Yes, there are solutions but they are worth perhaps more than a couple of lines of code in a forum... – peter.cyc Jan 17 '21 at 21:03
  • With regards to high performance, one way is to run Spacy-NLP as a Python web service. Swi-prolog client can then post requests and retreive answers. – peter.cyc Jan 18 '21 at 21:30
  • i was thinking if Swi can do IPC ! – sten Jan 18 '21 at 22:00
  • It does ! cf https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/tipc.html%27) There are some code examples on sending and receiving messages ! – peter.cyc Jan 19 '21 at 07:14
  • is this TIPC compatible with other languages IPC ? – sten Jan 20 '21 at 00:06