Skip to content

Commit

Permalink
Call init and shutdown thread safely (#508)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
  • Loading branch information
ivanpauno authored Feb 14, 2020
1 parent 822f194 commit 6d009fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 3 additions & 5 deletions rclpy/rclpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -68,9 +68,7 @@ def init(*, args: 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
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
Expand Down
15 changes: 15 additions & 0 deletions rclpy/rclpy/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -37,6 +40,18 @@ def __init__(self):
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:
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
Expand Down

0 comments on commit 6d009fb

Please sign in to comment.