Skip to content

Commit

Permalink
added better python APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Jun 7, 2024
1 parent 5a65e03 commit fc55d5a
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
81 changes: 81 additions & 0 deletions python/mochi/flock/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# (C) 2024 The University of Chicago
# See COPYRIGHT in top-level directory.


"""
.. module:: client
:synopsis: This package provides access to the Flock C++ wrapper
.. moduleauthor:: Matthieu Dorier <mdorier@anl.gov>
"""


import pyflock_common
import pyflock_client
import pymargo.core
import pymargo


class GroupHandle:

def __init__(self, internal, client):
self._internal = internal
self._client = client

@property
def client(self):
return self._client

def update(self):
self._internal.update()

def view(self):
return self._internal.view


class Client:

def __init__(self, arg):
if isinstance(arg, pymargo.core.Engine):
self._engine = arg
self._owns_engine = False
elif isinstance(arg, str):
self._engine = pymargo.core.Engine(arg, pymargo.client)
self._owns_engine = True
else:
raise TypeError(f'Invalid argument type {type(arg)}')
self._internal = pyflock_client.Client(self._engine.get_internal_mid())

def __del__(self):
del self._internal
if self._owns_engine:
self._engine.finalize()
del self._engine

@property
def mid(self):
return self._internal.margo_instance_id

@property
def engine(self):
return self._engine

def make_group_handle(self, address: str|pymargo.core.Address, provider_id: int = 0):
if isinstance(address, pymargo.core.Address):
address = str(address)
return GroupHandle(
self._internal.make_service_handle(address=address, provider_id=provider_id),
self)

def make_group_handle_from_file(self, filename: str):
return GroupHandle(
self._internal.make_group_handle_from_file(filename=filename),
self)

def make_group_handle_from_serialized(self, serialized: str):
return GroupHandle(
self._internal.make_group_handle_from_serialized(serialized=serialized),
self)

18 changes: 18 additions & 0 deletions python/mochi/flock/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# (C) 2024 The University of Chicago
# See COPYRIGHT in top-level directory.


"""
.. module:: view
:synopsis: This package provides access to the Flock C++ wrapper
.. moduleauthor:: Matthieu Dorier <mdorier@anl.gov>
"""


import pyflock_common


FlockException = pyflock_common.Exception
26 changes: 26 additions & 0 deletions python/mochi/flock/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# (C) 2024 The University of Chicago
# See COPYRIGHT in top-level directory.


"""
.. module:: server
:synopsis: This package provides access to the Flock C++ wrapper
.. moduleauthor:: Matthieu Dorier <mdorier@anl.gov>
"""


import pyflock_common
from .view import GroupView
import pyflock_server
import pymargo.core
import pymargo


class Provider:

def __init__(self, engine: pymargo.core.Engine, provider_id: int, config: str, initial_view: GroupView):
self._internal = pyflock_server.Provider(
self._engine.get_internal_mid(), provider_id, config, initial_view)
18 changes: 18 additions & 0 deletions python/mochi/flock/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# (C) 2024 The University of Chicago
# See COPYRIGHT in top-level directory.


"""
.. module:: view
:synopsis: This package provides access to the Flock C++ wrapper
.. moduleauthor:: Matthieu Dorier <mdorier@anl.gov>
"""


import pyflock_common


GroupView = pyflock_common.GroupView

0 comments on commit fc55d5a

Please sign in to comment.