ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Proper way of calling service in rospy

asked 2017-11-20 05:39:56 -0600

ravijoshi gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-11-20 05:46:41 -0600

gvdhoorn gravatar image

updated 2017-11-20 05:47:50 -0600

The key here is persistence. Both of the object as well as the connection. Persistence impacts both performance as well as rosbustness.

I think this is pretty well covered by the documentation of ServiceProxy.

To avoid duplicating content I'm referring you to my answer to #q108256.

edit flag offensive delete link more

Comments

Thanks a lot. I learned something new. The service is running on the same computer with 8GB RAM and Intel i7 x64 processor. I think that the best way is to declare it inside __init__ constructor as self.service = rospy.ServiceProxy(self.service_name, my_service, persistent=True). Isn't it?

ravijoshi gravatar image ravijoshi  ( 2017-11-20 05:58:36 -0600 )edit

I think you're implying with "on the same computer" that there should not be any problem in using persistent connections, but that would only consider network problems as a cause of failure. I'm not sure it can be assumed that locally running service servers will be less susceptible to crashes ..

gvdhoorn gravatar image gvdhoorn  ( 2017-11-20 06:01:02 -0600 )edit

.. or other problems with might result in them becoming unreachable than remote ones.

Persistent services will be more performant as they avoid renegotiation of the connection, but at the cost of potential brittleness. I'm not sure I can tell you whether in your specific case one would be better ..

gvdhoorn gravatar image gvdhoorn  ( 2017-11-20 06:02:46 -0600 )edit

.. than the other.

Btw: if you have a scenario where you find yourself calling a service periodically at a (relatively) high rate, it may be an indication that a topic (ie: dataflow, event-based) could be a more suitable design.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-20 06:03:47 -0600 )edit

Additionally: whether persistent services make sense from a performance perspective will also be dependent on the ratio of time spent for lookup & negotiation of the connection versus the time spent in the actual service implementation (ie: the part that does the actual work).

gvdhoorn gravatar image gvdhoorn  ( 2017-11-20 06:05:58 -0600 )edit

Thank you very much for the detailed explanation. I am thinking about it.

ravijoshi gravatar image ravijoshi  ( 2017-11-20 20:50:14 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-20 05:39:56 -0600

Seen: 3,068 times

Last updated: Nov 20 '17