Performance: Creating a service server
This affects the responsiveness and interaction speed of the robot system by managing how service requests are handled and responded to.
def handle_request(req): # offload heavy work to a separate thread or async import threading threading.Thread(target=heavy_work, args=(req,)).start() return Response(success=True) service = rospy.Service('my_service', MyService, handle_request) rospy.spin()
def handle_request(req): # heavy computation or blocking call import time time.sleep(5) return Response(success=True) service = rospy.Service('my_service', MyService, handle_request) rospy.spin()
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking service callback | N/A | N/A | N/A | [X] Bad |
| Non-blocking async callback | N/A | N/A | N/A | [OK] Good |
import rospy
from beginner_tutorials.srv import AddTwoInts, AddTwoIntsResponse
def handle_add_two_ints(req):
return AddTwoIntsResponse(req.a + req.b)
rospy.init_node('add_two_ints_server')
service = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
rospy.spin()import rospy
from beginner_tutorials.srv import AddTwoInts, AddTwoIntsResponse
def handle_add(req):
sum = req.a + req.b
rospy.init_node('add_two_ints_server')
service = rospy.Service('add_two_ints', AddTwoInts, handle_add)
rospy.spin()Pass the test and this pattern moves unknown