Skip to content

Commit

Permalink
Make subscriptions context-manager aware. (#1291)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Jun 3, 2024
1 parent 547245a commit ffcdae9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rclpy/rclpy/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from enum import Enum
import inspect
from typing import Callable, Generic, List, Type, TypeVar
from types import TracebackType
from typing import Callable, Generic, List, Optional, Type, TypeVar

from rclpy.callback_groups import CallbackGroup
from rclpy.event_handler import EventHandler, SubscriptionEventCallbacks
Expand Down Expand Up @@ -123,3 +124,14 @@ def callback(self, value: Callable[[MsgT], None]) -> None:
raise RuntimeError(
'Subscription.__init__(): callback should be either be callable with one argument'
'(to get only the message) or two (to get message and message info)')

def __enter__(self) -> 'Subscription':
return self

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
self.destroy()

0 comments on commit ffcdae9

Please sign in to comment.