-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make nodes, publishers, subscriptions, services, clients, action servers, action clients Python context managers #1280
Labels
Comments
This was referenced May 17, 2024
This was referenced Jun 4, 2024
This was referenced Jul 8, 2024
While there are still a few outstanding pull requests for this, this has largely been completed in Rolling. Thus I'm going to close this one out. Thanks in particular to @sloretz for all of the reviews in getting this in. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's say that you want to create an rclpy node with a publisher and a subscription. In order to do this entirely correctly today, you would have to do the following:
That is, every time we successfully create an entity, we really need to put a
try..finally
block around it to clean up only those parts that we've initialized so far. This is very unwieldy, so we don't actually recommend it in our examples or documentation.However, we should be able to do better. In particular, if all of the initialization, nodes, publishers, subscriptions, services, clients, action servers, and action clients were Python context managers (i.e. they all implemented
__enter__
and__exit__
), this could be cleaned up to something like:Or even better, using a contextlib.ExitStack:
This came out of discussion around ros2/examples#379
The text was updated successfully, but these errors were encountered: