Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Add option on rpc server for accepting multiple services #1074

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/1074.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option on rpc server for accepting multiple services
27 changes: 14 additions & 13 deletions src/ansys/mechanical/core/embedding/rpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,28 @@
class MechanicalService(rpyc.Service):
"""Starts Mechanical app services."""

def __init__(self, backgroundapp, functions=[], impl=None):
def __init__(self, backgroundapp, functions=[], impl=[]):
"""Initialize the service."""
super().__init__()
self._backgroundapp = backgroundapp
self._install_functions(functions)
self._install_class(impl)
self.EMBEDDED = True
self._install_classes(impl)

Check warning on line 50 in src/ansys/mechanical/core/embedding/rpc/server.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/rpc/server.py#L50

Added line #L50 was not covered by tests

def _install_functions(self, methods):
"""Install the given list of methods."""
if not methods:
return

Check warning on line 55 in src/ansys/mechanical/core/embedding/rpc/server.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/rpc/server.py#L54-L55

Added lines #L54 - L55 were not covered by tests
[self._install_function(method) for method in methods]

def _install_classes(self, impl):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo - class can be either a single class or a list.

"""Install the given list of classes."""
if not impl:
return
[self._install_class(_impl) for _impl in impl]

Check warning on line 62 in src/ansys/mechanical/core/embedding/rpc/server.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/rpc/server.py#L60-L62

Added lines #L60 - L62 were not covered by tests

def _install_class(self, impl):
"""Install methods from the given implemented class."""
print("Install class")
if impl is None:
return
print("Installing methods from class")
print(f"Installing methods from class : {impl}")

Check warning on line 66 in src/ansys/mechanical/core/embedding/rpc/server.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/rpc/server.py#L66

Added line #L66 was not covered by tests
for methodname, method, methodtype in get_remote_methods(impl):
print(f"installing {methodname} of {impl}")
if methodtype == MethodType.METHOD:
Expand Down Expand Up @@ -224,20 +228,17 @@
port: int = None,
version: int = None,
methods: typing.List[typing.Callable] = [],
impl=None,
impl: typing.List[typing.Callable] = [],
):
"""Initialize the server."""
self._exited = False
self._background_app = BackgroundApp(version=version)
self._service = service
self._methods = methods
self._methods = methods if methods is not None else []
print("Initializing Mechanical ...")

self._port = self.get_free_port(port)
if impl is None:
self._impl = None
else:
self._impl = impl(self._background_app.app)
self._impl = [i(self._background_app.app) for i in impl] if impl else []

my_service = self._service(self._background_app, self._methods, self._impl)
self._server = ThreadedServer(my_service, port=self._port)
Expand Down
Loading