SWI-Prolog Python interface
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog Python interface
        • Introduction
        • Data conversion
        • Janus by example - Prolog calling Python
        • library(janus): Call Python from Prolog
        • Calling Prolog from Python
        • Janus and threads
        • Janus and signals
        • Janus versions
        • Janus as a Python package
        • Prolog and Python
        • Janus performance evaluation
        • Python or C/C++ for accessing resources?
        • Janus platforms notes
        • Compatibility to the XSB Janus implementation
        • Status of Janus

7 Janus and signals

If Prolog is embedded into Python, SWI-Prolog is started with the --no-signals, i.e., SWI-Prolog does not install any signal handlers. This implies that signals are handled by Python. Python handles signals synchronously (as SWI-Prolog) when executing byte code. As Prolog execution does not involve Prolog execution, running a program like below cannot be in interrupted

import janus_swi as janus
janus.query_once("repeat,fail")

If your program makes possibly slow Prolog queries and you want signal handling, you can enable a heartbeat.

None janus.heartbeat(count=10000)
Ask Prolog to call a dummy function every count inferences. This allows Python to handle signals. Lower numbers for count improve responsiveness at the cost of slowing down Prolog. Note that Prolog calls to foreign code count as one inference. Signal handling is completely blocked if Prolog is blocked in foreign code.

To complete the picture, some Python exceptions are propagated through Prolog by mapping them into a Prolog exception and back again. This notably concerns

SystemExit(code)
This Python exception is mapped to the Prolog exception unwind(halt(code)) and back again when Prolog returns control back to Python.
KeyboardInterrupt
This Python exception is mapped to the Prolog exception unwind(keyboard_interrupt)