RSS

A Python API for Ouroboros

Python

Support for other programming languages than C/C++ has been on my todo list for quite some time. The initial approach was using SWIG, but we found the conversion always clunky, it didn’t completely work as we wanted to, and a while back we just decided to deprecate it. Apart from C/C++ we only had a rust wrapper.

Until now! I finally took the time to sink my teeth into the bindings for Python. I had some brief looks at the ctypes library a while back, but this time I looked into cffi and I was amazed at how simple it was to wrap the more difficult functions that manipulate blocks of memory (flow_read, but definitely the async fevent() call). And now there is path towards a ‘nice’ Python API.

Here is a taste of what the oecho tool looks like in Python:

from ouroboros import *
import argparse


def client():
    f = flow_alloc("oecho")
    f.writeline("Hello, PyOuroboros!")
    print(f.readline())
    f.dealloc()


def server():
    print("Starting the server.")
    while True:
        f = flow_accept()
        print("New flow.")
        line = f.readline()
        print("Message from client is " + line)
        f.writeline(line)
        f.dealloc()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='A simple echo client/server')
    parser.add_argument('-l', '--listen', help='run as a server', action='store_true')
    args = parser.parse_args()
    if args.listen is True:
        server()
    else:
        client()

I have more time in the next couple of days, so I expect this to be released after the weekend.

Oh, and here is a picture of Ouroboros load-balancing between the C (top right) and Python (top left) implementations using the C and Python clients:

Can’t wait to get the full API done!

Cheers,

Dimitri