Proper way of calling service in rospy
I have designed a service and I am using it without any problem. However, I am having doubt about the client implementation in rospy. I am calling this service many times and hence, I am concerned about its implementation. Please see the code snippet below:
class MyServiceClient:
def __init__(self):
self.service_name = 'my_service'
rospy.wait_for_service(self.service_name)
def response(self):
try:
service = rospy.ServiceProxy(self.service_name, my_service)
service_response = service(my_param)
return service_response.data
except rospy.ServiceException, e:
print 'Service call failed: %s' % e
return None
my_service_client = MyServiceClient()
for i in xrange(100):
response = my_service_client.response()
I want to know if creating service proxy object everytime is a good choice or not? Or should it be a class variable declared inside __init__
constructor as self.service = rospy.ServiceProxy(self.service_name, my_service)
.
Please note that I am using Python 2.7 in ROS Indigo on Ubuntu 14.04 LTS PC. Thank you very much.