Calling the core through ctypes
The service starts a process for every request. It is simple, it isolates the COBOL completely, and it costs a few milliseconds. The same logic can also be loaded into the Python process as a shared module and called like a C function, which is the second way this project could talk to its core. Both were built here, and the page ends with the measurement that decides between them.
The core as a module
The core file already holds PRICECORE as a separate program, so the module build is one command, and the file that comes out exports that program\'s entry point:
cobc -m -free -Wall -O2 -o PRICECORE.so price.cob
ls -l PRICECORE.so
-rwxr-xr-x 1 agent agent 18496 Nov 18 17:31 PRICECORE.so
The COBOL below is the unit of the core page, unchanged: the same validation, the same rounding, the same status codes. That is the point of splitting the unit out of the command in the first place: one source, two ways in.
One call, no process
Four things have to be right before the first call works, and each of them cost a failure here. cob_init has to be called once from libcob, or the module stops with cob_init() has not been called. The argument types have to be declared as pointers, or ctypes passes 32-bit integers and the module writes through a truncated address and the process dies. The request and the answer are buffers of the record length, and the module is called with their addresses. The program keeps its own storage between calls, so the unit must not depend on anything left in working storage, which it does not.
#!/usr/bin/env python3
"""Call the shared COBOL module directly, without starting a process."""
import ctypes
REQUEST = b"C00001WIDGET0100002000123456N1"
ANSWER_SIZE = 50
ctypes.CDLL("libcob.so.4").cob_init(0, None)
module = ctypes.CDLL("./PRICECORE.so")
module.PRICECORE.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
module.PRICECORE.restype = ctypes.c_int
request = ctypes.create_string_buffer(REQUEST, 30)
answer = ctypes.create_string_buffer(ANSWER_SIZE)
module.PRICECORE(ctypes.addressof(request), ctypes.addressof(answer))
print(answer.raw[:ANSWER_SIZE].decode("ascii").rstrip())
python3 -I -m py_compile call_core.py
python3 -I call_core.py
echo $?
C00001000000024691200000032099PRICED
0
The benchmark
The two paths measured with the same request: a subprocess per call, as the service does it, and a ctypes call into the loaded module. The subprocess rounds run a hundred calls, the ctypes rounds ten thousand, because one subprocess call is about two thousand times a ctypes call. Five rounds each, one warm-up round each before them, and the median of the five:
#!/usr/bin/env python3
"""Compare one call to the core as a subprocess with one call through ctypes."""
import ctypes
import statistics
import subprocess
import time
REQUEST = b"C00001WIDGET0100002000123456N1"
ROUNDS = 5
SUBPROCESS_CALLS = 100
CTYPES_CALLS = 10000
def subprocess_round():
started = time.perf_counter()
for _ in range(SUBPROCESS_CALLS):
subprocess.run(["./price"], input=REQUEST, capture_output=True, check=False)
return (time.perf_counter() - started) / SUBPROCESS_CALLS
def ctypes_round(lib, request, answer):
started = time.perf_counter()
for _ in range(CTYPES_CALLS):
lib.PRICECORE(ctypes.addressof(request), ctypes.addressof(answer))
return (time.perf_counter() - started) / CTYPES_CALLS
def main():
libcob = ctypes.CDLL("libcob.so.4")
libcob.cob_init(0, None)
lib = ctypes.CDLL("./PRICECORE.so")
lib.PRICECORE.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
lib.PRICECORE.restype = ctypes.c_int
request = ctypes.create_string_buffer(REQUEST, 30)
answer = ctypes.create_string_buffer(50)
subprocess_round()
ctypes_round(lib, request, answer)
subprocess_times = [subprocess_round() for _ in range(ROUNDS)]
ctypes_times = [ctypes_round(lib, request, answer) for _ in range(ROUNDS)]
print("subprocess, 100 calls per round, seconds per call:")
for value in subprocess_times:
print(f" {value * 1000:8.3f} ms")
print("ctypes, 10000 calls per round, seconds per call:")
for value in ctypes_times:
print(f" {value * 1e6:8.3f} us")
print(f"median subprocess {statistics.median(subprocess_times) * 1000:.3f} ms")
print(f"median ctypes {statistics.median(ctypes_times) * 1e6:.3f} us")
ratio = statistics.median(subprocess_times) / statistics.median(ctypes_times)
print(f"ratio {ratio:.0f} times")
if __name__ == "__main__":
main()
python3 -I -m py_compile bench.py
python3 -I bench.py
echo $?
subprocess, 100 calls per round, seconds per call:
5.232 ms
4.597 ms
4.431 ms
4.268 ms
4.408 ms
ctypes, 10000 calls per round, seconds per call:
2.303 us
2.127 us
2.131 us
2.120 us
2.105 us
median subprocess 4.431 ms
median ctypes 2.127 us
ratio 2083 times
0
The numbers
| Path | Per call, median | What it pays for |
|---|---|---|
| subprocess | 4.431 ms | fork, exec, load the COBOL runtime, read and write two pipes, exit |
| ctypes | 2.127 microseconds | a C function call and the COBOL unit\'s own work |
The first ctypes round is visibly slower than the other four, 2.303 microseconds against 2.1, because the module and its runtime are being touched for the first time in the process; the median ignores it. The subprocess set is steady at about 4.3 milliseconds, which is the price of starting a process, not of pricing an order: the unit\'s own work is two multiplications and a few moves. A second run of the same script on this machine gave 4.381 milliseconds and 2.073 microseconds, a ratio of 2113: that spread of a few percent is the shared host, and it is why the page quotes a median of five rounds and not one number.
What the measurement does not say: the two paths are not equally safe. A subprocess cannot corrupt the Python process and a ctypes call can, because the COBOL runtime writes into memory the Python process owns. The subprocess can be killed on a timeout; a ctypes call that loops forever hangs the service. And the subprocess loads the runtime per call, so the 4 milliseconds is also a fresh runtime each time, which a stateful core would need.
Which one to use
For this project the subprocess is the right default at 4.3 milliseconds per order: an HTTP request costs more than that in the network, and the isolation is worth the milliseconds. The ctypes path becomes interesting in three cases, and each one is a fact about the workload rather than taste:
- A batch of thousands of calls in one process. Ten thousand calls cost 43 seconds as subprocesses and 22 milliseconds through ctypes; that is the difference between a nightly job of an hour and one of a second.
- A latency budget below a millisecond. A pricing engine inside a matching loop cannot afford a fork, and the 4 milliseconds is the whole budget.
- The core is a pure function of its record. The ctypes path is only safe when the unit keeps nothing between calls and cannot corrupt anything, which is the property the core page was written to have.
Everything else favours the process: a crash is a 500 and not a dead service, a timeout is a 504, and the COBOL can be rebuilt and replaced without restarting Python. The next page packages the whole project as a container image, and the page after it says what a production version would change, with these numbers in hand.
The COBOL was built with cobc (GnuCOBOL) 3.2.0 with -O2, the Python is Python 3.14.10, and the benchmark ran on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4. The timings are wall time of this container, measured with time.perf_counter, five rounds each and the median quoted.
Article author: Arthur Isaev