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

Avoid reference cycle between Node and ParameterService #490

Merged
merged 4 commits into from
Jan 7, 2020
Merged
Changes from 2 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
77 changes: 46 additions & 31 deletions rclpy/rclpy/parameter_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import weakref

from rcl_interfaces.msg import ParameterDescriptor
from rcl_interfaces.msg import SetParametersResult
from rcl_interfaces.srv import DescribeParameters, GetParameters, GetParameterTypes
Expand All @@ -25,7 +27,7 @@
class ParameterService:

def __init__(self, node):
self._node = node
self._node_weak_ref = weakref.ref(node)
nodename = node.get_name()

describe_parameters_service_name = \
Expand Down Expand Up @@ -64,28 +66,37 @@ def __init__(self, node):
)

def _describe_parameters_callback(self, request, response):
for name in request.names:
try:
descriptor = self._node.describe_parameter(name)
except ParameterNotDeclaredException:
descriptor = ParameterDescriptor()
response.descriptors.append(descriptor)
node = self._node_weak_ref()
if node is not None:
dirk-thomas marked this conversation as resolved.
Show resolved Hide resolved
for name in request.names:
try:
descriptor = node.describe_parameter(name)
except ParameterNotDeclaredException:
descriptor = ParameterDescriptor()
response.descriptors.append(descriptor)
return response

def _get_parameters_callback(self, request, response):
for name in request.names:
p = self._node.get_parameter_or(name)
response.values.append(p.get_parameter_value())
node = self._node_weak_ref()
if node is not None:
for name in request.names:
p = node.get_parameter_or(name)
response.values.append(p.get_parameter_value())
return response

def _get_parameter_types_callback(self, request, response):
for name in request.names:
response.types.append(self._node.get_parameter_or(name).type_)
node = self._node_weak_ref()
if node is not None:
for name in request.names:
response.types.append(node.get_parameter_or(name).type_)
return response

def _list_parameters_callback(self, request, response):
names_with_prefixes = []
for name in self._node._parameters.keys():
node = self._node_weak_ref()
if node is None:
return response
for name in node._parameters.keys():
if PARAMETER_SEPARATOR_STRING in name:
names_with_prefixes.append(name)
continue
Expand Down Expand Up @@ -125,25 +136,29 @@ def _list_parameters_callback(self, request, response):
return response

def _set_parameters_callback(self, request, response):
for p in request.parameters:
param = Parameter.from_parameter_msg(p)
try:
result = self._node.set_parameters_atomically([param])
except ParameterNotDeclaredException as e:
result = SetParametersResult(
successful=False,
reason=str(e)
)
response.results.append(result)
node = self._node_weak_ref()
if node is not None:
for p in request.parameters:
param = Parameter.from_parameter_msg(p)
try:
result = node.set_parameters_atomically([param])
except ParameterNotDeclaredException as e:
result = SetParametersResult(
successful=False,
reason=str(e)
)
response.results.append(result)
return response

def _set_parameters_atomically_callback(self, request, response):
try:
response.result = self._node.set_parameters_atomically([
Parameter.from_parameter_msg(p) for p in request.parameters])
except ParameterNotDeclaredException as e:
response.result = SetParametersResult(
successful=False,
reason=str(e)
)
node = self._node_weak_ref()
if node is not None:
try:
response.result = node.set_parameters_atomically([
Parameter.from_parameter_msg(p) for p in request.parameters])
except ParameterNotDeclaredException as e:
response.result = SetParametersResult(
successful=False,
reason=str(e)
)
return response