我现在成功了 . 我的想法是为行动的 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()
2 回答
请记住,asyncio意味着在单个线程中运行 .
如果程序需要与其他线程交互,则必须使用其中一个专用函数:
loop.call_soon_threadsafe
asyncio.run_coroutine_threadsafe
这是一个简化的例子:
或者,loop.run_in_executor提供了一种通过在自己的线程中运行给定函数来与非asyncio库交互的方法:
随着文森特的想法,
我实际上找到了解决问题的方法:
如果有人知道如何以更智能的方式实现它,请与使用者分享这些知识 .
最好的曼努埃尔