- Documentation
- Reference manual
- Packages
- SWI-Prolog Python interface
- Introduction
- Data conversion
- Janus by example
- library(janus): Call Python from Prolog
- Calling Prolog from Python
- Janus and threads
- 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
- SWI-Prolog Python interface
6 Janus and threads
Where SWI-Prolog support native preemptively scheduled threads that exploit multiple cores, Python has a single interpreter that can switch between native threads.6Actually, you can create multiple Python interpreters. It is not yet clear to us whether that can help improving on concurrency. Initially the Python interpreter is associated with the thread that created it which, for janus, is the first thread calling Python. Janus uses PyGILState_Ensure() and PyGILState_Release() around calls to e.g. py_call/2. In addition, the thread that created Python releases its interpreter after every call from Prolog on Python. As a result:
- Multiple Prolog threads can make calls to Python. The access to
Python is serialized. If a Prolog thread does not want other
threads to use Python it can use
py_with_gil/1.
When multiple Prolog threads make many calls to Python performance tends
to drop significantly.
- Multiple Python threads can make calls to Prolog. As Python uses
only one core at the same time, Prolog working on behalve of Python synchronously
only uses one core. Prolog threads not related to Python can proceed on
other cores.
- Python threading is cooperative. At the moment, a thread
Python thread running Prolog will not allow other Python threads to make
progress. Possibly that can be improved in the future.
- It appears to be possible to initialize Python in a thread. Python remains accessible from other threads after the initializing thread has terminated.