Skip to content

Commit

Permalink
Make publishers context-manager aware. (#1289)
Browse files Browse the repository at this point in the history
The main reason to do this is so that we can change example
code to use context managers for managing the lifetimes
of publishers (and other entities), which should make the
code significantly easier to read.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
  • Loading branch information
clalancette authored May 31, 2024
1 parent 786c464 commit 547245a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rclpy/rclpy/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Generic, List, Type, TypeVar, Union
from types import TracebackType
from typing import Generic, List, Optional, Type, TypeVar, Union

from rclpy.callback_groups import CallbackGroup
from rclpy.duration import Duration
Expand Down Expand Up @@ -128,3 +129,14 @@ def wait_for_all_acked(self, timeout: Duration = Duration(seconds=-1)) -> bool:
"""
with self.handle:
return self.__publisher.wait_for_all_acked(timeout._duration_handle)

def __enter__(self) -> 'Publisher':
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 547245a

Please sign in to comment.