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

client must wait until service callback finish?

asked 2022-06-10 01:44:47 -0500

RyanChen.YLC gravatar image

As title, I have the following two python codes,

#!/usr/bin/env python

from __future__ import print_function

import sys
import rospy
from test_srv.srv import *

def client_send(x, y):
    rospy.wait_for_service('test_server')
    try:

        t1 = rospy.get_time()
        c = rospy.ServiceProxy('test_server', test)
        c(x, y)
        print("Run Time: ", (rospy.get_time() - t1))


    except rospy.ServiceException as e:
        print("Service call failed: %s"%e)

def do_something():

    print("Doing something...")

if __name__ == "__main__":

    rospy.init_node('test_server_node_1')

    x = 10
    y = 11

    print("Client: Start sending.")
    client_send(x, y)
    do_something()

and

#!/usr/bin/env python

from __future__ import print_function
from time import sleep
from test_srv.srv import test, testResponse
import rospy

def handleCB(req):
    print("start sleep")
    sleep(3)
    print("Get request: %s and %s" %(req.a, req.b))
    return testResponse()


def test_server():
    rospy.init_node('test_server_node')
    s = rospy.Service('test_server',test, handleCB)
    print("Ready.")
    rospy.spin()

if __name__ == "__main__":
    test_server()

the srv file:

int64 a
int64 b
---

When running these codes, the client will keep waiting until server finishes its handleCB function.

My question is that can client send request and then do other things, not just wait for the server sending response back?

Thanks.

edit retag flag offensive close merge delete

Comments

Update:

running result: see image

RyanChen.YLC gravatar image RyanChen.YLC  ( 2022-06-10 01:49:48 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-10 07:49:44 -0500

Alex-SSoM gravatar image

Services are synchronous, therefore your client will wait for an answer before resuming execution. Topics are asynchronous, which means you can send a request and the client can continue the execution. To get a similar behaviour to a service but async, you'll need one topic to send the request and one topic to listen for an answer.

edit flag offensive delete link more

Comments

1

RyanChen: rather than make up your own protocol, you could look at http://wiki.ros.org/actionlib, which is one approach to do what you want. Notice that SimpleActionClient and SimpleActionServer have limitations (to keep them "simple").

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-06-10 09:39:00 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-06-10 01:44:47 -0500

Seen: 199 times

Last updated: Jun 10 '22