user3851038
user3851038

Reputation: 85

python3 asyncio await on callback

I'm trying to figure out how to use rospy actionlib and asyncio to wait asynchronously on the action result. For this purpose I tried to write an action execution generator but haven't succeeded now. My idea was to add a asyncio future to the done_callback of the action but the future seems to be never done in the end.

The code is here:

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()

Upvotes: 2

Views: 5345

Answers (3)

Vincent
Vincent

Reputation: 13415

Keep in mind that asyncio is meant to run in a single thread.

If the program needs to interact with other threads, you'll have to use one of the dedicated functions:

Here's a simplified example:

async def run_with_non_asyncio_lib(action, arg):
    future = asyncio.Future()
    loop = asyncio.get_event_loop()

    def callback(*args):
        loop.call_soon_threasafe(future.set_result, args)

    non_asyncio_lib.register_callback(action, arg, callback)
    callback_args = await future
    return process(*callback_args)

Alternatively, loop.run_in_executor provides a way to interact with non-asyncio libraries by running the given function in its own thread:

async def run_with_non_asyncio_lib(action, arg):
    loop = asyncio.get_event_loop()
    future = loop.run_in_executor(None, non_asyncio_lib.run, action, arg)
    result = await future
    return result

Upvotes: 1

yurenchen
yurenchen

Reputation: 2483

a full test code (for need user)
// base on @user3851038 's answer and
// asyncio.wait_for() src implement



import asyncio
# from asyncio import exceptions
from datetime import datetime

# wait a callback called
async def test_wait_callback():
    loop = asyncio.get_event_loop()
    waiter = loop.create_future()

    ret_code = None
    def callback():
        nonlocal ret_code
        ret_code = 123
        print(datetime.now(), 'callback called:', ret_code)
        if not waiter.done(): waiter.set_result(None)  # <--- tell done

    print(datetime.now(), 'do_stuff_with_callback call')
    await do_stuff_with_callback(callback)
    print(datetime.now(), 'do_stuff_with_callback return')

    try:
        ret = await waiter                             # <--- wait done
        print(datetime.now(), 'callback done  :', ret_code)
    except asyncio.exceptions.CancelledError:
        pass


# run something in main loop (not current task), with callback
async def do_stuff_with_callback(cb):
    async def task():
        await asyncio.sleep(2) # 2 sec
        if callable(cb): cb()

    loop = asyncio.get_event_loop()
    loop.create_task(task())


if __name__ == '__main__':
    asyncio.run(test_wait_callback())

    # loop = asyncio.get_event_loop()
    # loop.run_until_complete(test_wait_callback())



output:

2021-12-04 18:09:36.400958 do_stuff_with_callback call
2021-12-04 18:09:36.400996 do_stuff_with_callback return
2021-12-04 18:09:38.401790 callback called: 123
2021-12-04 18:09:38.401974 callback done  : 123

Upvotes: 0

user3851038
user3851038

Reputation: 85

With the idea of Vincent,

I actually found a solution for my problem:

def _generate_action_executor(action):
    async def run_action(goal):
        loop = asyncio.get_event_loop()
        action_done = loop.create_future()

        def done_callback(goal_status, result, future, loop):
            status = ActionLibGoalStatus(goal_status)
            print('Action Done: {}'.format(status))
            loop.call_soon_threadsafe(future.set_result(result))

        action.send_goal(goal,partial(done_callback, future=action_done, loop=loop))
        try:
            await action_done
        except asyncio.CancelledError as exc:
            action.cancel()
            raise exc

        return action_done.result()

    return run_action

If someone know how to implement it in a smarter fashion please share that knowledge with use.

Best Manuel

Upvotes: 2

Related Questions