papi: vpp_papi asyncio support 72/41972/3
authorOle Troan <[email protected]>
Fri, 6 Dec 2024 15:49:25 +0000 (16:49 +0100)
committerDamjan Marion <[email protected]>
Mon, 16 Dec 2024 10:02:48 +0000 (10:02 +0000)
commit0ad98a8c9d36fe2363e9bb6a23a500c4097b25fc
treeafdd7c47208a2e31c09b609887d8188671982d46
parentd8bab19b8dd3229baf0d0b87c68b3a19f2c53bfb
papi: vpp_papi asyncio support

An asyncio version of the VPP Python API.
A API call returns a awaitable future.
In comparision to the legacy API, the extra message receive thread
is no needed.

from vpp_papi.vpp_papi_async import VPPApiClient

async def process_events(event_queue):
    while True:
        event = await event_queue.get()
        print(f"*** Processing event: {event}")
        if event is None:
            return

async def test():
    vpp = VPPApiClient()
    event_queue = asyncio.Queue()
    event_processor_task = asyncio.create_task(process_events(event_queue))

    rv = await vpp.connect("foobar", event_queue)
    assert rv == 0
    rv = await vpp.api.show_version()
    rv = await vpp.api.sw_interface_dump()

    await event_queue.put(None)    # Send sentinel to stop the event processor
    await asyncio.gather(event_processor_task)  # Wait for them to finish

    await vpp.disconnect()

Example of sending multiple requests and gather replies asynchronously
async def test_bulk():
    futures = []
    for i in range(n):
        futures.append(vpp.api.show_version())
    rv = await asyncio.gather(*futures)

def main():
    asyncio.run(test())

Type: feature
Change-Id: Ie6bcb483930216c21a45658b72e87ba4c46f43ad
Signed-off-by: Ole Troan <[email protected]>
src/vpp-api/python/setup.py
src/vpp-api/python/vpp_papi/vpp_papi_async.py [new file with mode: 0644]