我试图弄清楚如何使用 rospy actionlib 和 asyncio 来异步等待操作结果。为此,我尝试编写一个动作执行生成器,但现在还没有成功。我的想法是为done_callback
动作添加一个 asyncio 未来,但未来似乎永远不会最终完成。
代码在这里:
def _generate_action_executor(self, action):
async def run_action(goal):
action_done = asyncio.Future()
def done_callback(goal_status, result, future):
status = ActionLibGoalStatus(goal_status)
print('Action Done: {}'.format(status))
future.set_result(result)
action.send_goal(goal,
lambda x, y: done_callback(x,
y,
action_done))
try:
result = await action_done
#problem future never done
except asyncio.CancelledError as exc:
action.cancel()
raise exc
return result
return run_action
async def do_some_other_stuff(action):
//do stuff
my_goal = MyActionRequest('just do it')
run_action = self._generate_action_executor(action)
response = await run_action(my_goal)
return response
if __name__ == "__main__":
action = actionlib.SimpleActionClient('my_action',
MyAction)
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(do_some_other_stuff(action))
finally:
loop.close()