From 064f3d9b0468469dd091df1170837b12ed1928c7 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Wed, 12 Feb 2020 18:21:48 -0300 Subject: [PATCH 1/3] Call init and shutdown thread safely Signed-off-by: Ivan Santiago Paunovic --- rclpy/rclpy/__init__.py | 7 +++---- rclpy/rclpy/context.py | 9 +++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/rclpy/rclpy/__init__.py b/rclpy/rclpy/__init__.py index 83b2e14d3..e9084d247 100644 --- a/rclpy/rclpy/__init__.py +++ b/rclpy/rclpy/__init__.py @@ -40,8 +40,8 @@ This will invalidate all entities derived from the context. """ -import sys from typing import List +from typing import Optional from typing import TYPE_CHECKING from rclpy.context import Context @@ -59,7 +59,7 @@ from rclpy.node import Node # noqa: F401 -def init(*, args: List[str] = None, context: Context = None) -> None: +def init(*, args: Optional[List[str]] = None, context: Context = None) -> None: """ Initialize ROS communications for a given context. @@ -69,8 +69,7 @@ def init(*, args: List[str] = None, context: Context = None) -> None: """ context = get_default_context() if context is None else context # imported locally to avoid loading extensions on module import - from rclpy.impl.implementation_singleton import rclpy_implementation - return rclpy_implementation.rclpy_init(args if args is not None else sys.argv, context.handle) + return context.init(args) # The global spin functions need an executor to do the work diff --git a/rclpy/rclpy/context.py b/rclpy/rclpy/context.py index b18542732..105340730 100644 --- a/rclpy/rclpy/context.py +++ b/rclpy/rclpy/context.py @@ -12,8 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys import threading from typing import Callable +from typing import List +from typing import Optional import weakref @@ -37,6 +40,12 @@ def __init__(self): def handle(self): return self._handle + def init(self, args: Optional[List[str]] = None): + from rclpy.impl.implementation_singleton import rclpy_implementation + with self._lock: + rclpy_implementation.rclpy_init( + args if args is not None else sys.argv, self._handle) + def ok(self): """Check if context hasn't been shut down.""" # imported locally to avoid loading extensions on module import From 2c8a6a78c8d2b92648b13e32f98b415b12b18513 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 13 Feb 2020 11:14:01 -0300 Subject: [PATCH 2/3] Address peer review comments Signed-off-by: Ivan Santiago Paunovic --- rclpy/rclpy/__init__.py | 1 - rclpy/rclpy/context.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/rclpy/rclpy/__init__.py b/rclpy/rclpy/__init__.py index e9084d247..0b6ece4c2 100644 --- a/rclpy/rclpy/__init__.py +++ b/rclpy/rclpy/__init__.py @@ -68,7 +68,6 @@ def init(*, args: Optional[List[str]] = None, context: Context = None) -> None: (see :func:`.get_default_context`). """ context = get_default_context() if context is None else context - # imported locally to avoid loading extensions on module import return context.init(args) diff --git a/rclpy/rclpy/context.py b/rclpy/rclpy/context.py index 105340730..b8ebe58d8 100644 --- a/rclpy/rclpy/context.py +++ b/rclpy/rclpy/context.py @@ -41,6 +41,7 @@ def handle(self): return self._handle def init(self, args: Optional[List[str]] = None): + # imported locally to avoid loading extensions on module import from rclpy.impl.implementation_singleton import rclpy_implementation with self._lock: rclpy_implementation.rclpy_init( From 3c44e03369bf80953eec62a81ca6d88cc0300b5a Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 13 Feb 2020 11:14:28 -0300 Subject: [PATCH 3/3] Add documentation Signed-off-by: Ivan Santiago Paunovic --- rclpy/rclpy/context.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rclpy/rclpy/context.py b/rclpy/rclpy/context.py index b8ebe58d8..22defea3b 100644 --- a/rclpy/rclpy/context.py +++ b/rclpy/rclpy/context.py @@ -41,6 +41,11 @@ def handle(self): return self._handle def init(self, args: Optional[List[str]] = None): + """ + Initialize ROS communications for a given context. + + :param args: List of command line arguments. + """ # imported locally to avoid loading extensions on module import from rclpy.impl.implementation_singleton import rclpy_implementation with self._lock: