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

Blocking service callbacks

asked 2014-04-23 06:13:45 -0600

crpizarr gravatar image

Hello everyone. I have been doing experiments with ROS services and currently I have this:

"""services_node.py"""
#!/usr/bin/env python

import time
import rospy
from beginner_tutorials.srv import * 

def handle_action_1(request):
    print "doing action 1"
    time.sleep(2)
    return Action1Response()

def handle_action_2(request):
    print "doing action 2"
    return Action2Response()

if __name__ == "__main__":
    rospy.init_node("services_node")
    action_1 = rospy.Service("action_1", Action1, handle_action_1)
    action_2 = rospy.Service("action_2", Action2, handle_action_2)
    rospy.spin()


"""caller_1.py"""
#!/usr/bin/env python

import rospy
from beginner_tutorials.srv import *

rospy.wait_for_service("action_2")
action_2 = rospy.ServiceProxy('action_2', Action2)
i = 0
while True:
    print "take %d" % i
    print "calling for action_2" 
    action_2()
    i = i + 1

"""caller_2.py"""
#!/usr/bin/env python

import rospy
from beginner_tutorials.srv import *

rospy.wait_for_service("action_1")
action_1 = rospy.ServiceProxy('action_1', Action1)
i = 0
while True:
    print "take %d" % i
    print "calling for action_1" 
    action_1()
    i = i + 1

This nodes are basically a node that publishes two services, called "action_1" and "action_2", and two nodes that constantly call those services. By the way, the Action1 and Action2 services are empty services, with just a "---" in the .srv files.

The problem I see is that when I run these nodes, the caller_1 node shows the "calling for action_1" every two seconds, which makes sense for me. But, as far as I know, I would expect the caller_2 node to print its text every two seconds, since the services_node is "busy" with the callback for action_1. But what I see is that the caller_2 node works as if was alone, without waiting for the response.

Can you explain this please? I thought service callbacks were blocking, since it's just one node who is answering those services, but it seems the callbacks are not blocking in this case.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-04-23 07:00:01 -0600

ahendrix gravatar image

Service calls are blocking in the sense that the caller will block until the server returns a response, and that the server for a particular service will only handle one request at a time.

I don't believe there's anything in the specification that says that two different services advertised from the same node will block each other. In reality, I suspect that rospy handles each service in its own thread, so that they don't block each other.

edit flag offensive delete link more

Comments

Given that, is there a way to make a service callback block other service callbacks? I ask because in the real project (not these dummy nodes) all the callbacks have to write in a TCP connection, so I need to make the callbacks exclusive in respect to each other. I know there are synchonization primitves as mutexes and stuff, but I would want to know if ROS has another way to do that.

crpizarr gravatar image crpizarr  ( 2014-04-23 08:01:41 -0600 )edit

If you're trying to control access to a raw object, you should use the python synchronization primitives.

ahendrix gravatar image ahendrix  ( 2014-04-23 08:10:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-04-23 06:13:45 -0600

Seen: 4,551 times

Last updated: Apr 23 '14