Skip to content
Merged
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
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