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

How can I check if a service exists from Python?

asked 2017-06-01 12:30:27 -0600

BJP gravatar image

updated 2017-06-01 13:37:19 -0600

This question has an answer for roscpp; what is the equivalent solution for Python?

EDIT: I do not want to wait for the service. I want to take one action if the service exists and a different action if it doesn't exist.

A hack might be to wrap a wait_for_service call in a try/catch to catch a ROSException and verify that that ROSException was a timeout, but that's very ugly and I'm not sure what the minimum required timeout should be to ensure that extant topics do not cause the Exception.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2017-06-02 00:59:25 -0600

updated 2017-06-03 04:14:33 -0600

There is a function in the rospy API to get a list of services: http://docs.ros.org/kinetic/api/rospy...

I guess this would return a list of all registered services and you could check if the service you are waiting for is in this list.

Edit: I haven't tested the API call I linked in the original answer. I used http://docs.ros.org/kinetic/api/rospy...

It works for me:

>>> import rosservice
>>> service_list = rosservice.get_service_list()
>>> print service_list
['/rosout/set_logger_level', '/rosout/get_loggers']
edit flag offensive delete link more

Comments

Have you tried this?

NEngelhard gravatar image NEngelhard  ( 2017-06-03 03:48:14 -0600 )edit

@NEngelhard I had successfully gotten a list of services in rospy, but now I realize it wasn't with this particular API call. I used this call: http://docs.ros.org/kinetic/api/rosse... . I should edit my answer accordingly.

Zubin Priyansh gravatar image Zubin Priyansh  ( 2017-06-03 04:10:13 -0600 )edit
0

answered 2017-06-01 12:43:19 -0600

NEngelhard gravatar image

updated 2017-06-01 12:43:55 -0600

Please at least read the wiki page (wiki.ros: Services) before opening a question here. The section

"(...) Wait until a service becomes available. You can optionally specify a timeout (in seconds) to wait for, in which case a ROSException is raised if the timeout is exceeded."

is hard to miss.

edit flag offensive delete link more

Comments

That doesn't answer the question. I want to take one action if a service is available and a different action if the service is not available.

BJP gravatar image BJP  ( 2017-06-01 13:33:59 -0600 )edit

Why do you think that the approach is "very ugly"? If you want to speed up the test, you still can look at the implementation and use the rosgraph.Master directly.

NEngelhard gravatar image NEngelhard  ( 2017-06-01 13:54:17 -0600 )edit

It's very ugly for (at least) three reasons: 1) that's not what wait_for_service is designed to do, 2) it's bad practice to catch an exception in unexceptional circumstances (when no error has occurred), and 3) the timeout duration selection issue.

BJP gravatar image BJP  ( 2017-06-01 14:23:05 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-06-01 12:30:27 -0600

Seen: 5,217 times

Last updated: Jun 03 '17