Skip to main content

An async library for XenAPI

Project description

async-xenapi (Python)

An async library for XenAPI.

Install

pip install async-xenapi

Or with uv:

uv add async-xenapi

Requires Python 3.12+.

Usage

import asyncio
from async_xenapi import AsyncXenAPISession

async def main():
    session = AsyncXenAPISession("https://xen-host")
    await session.login_with_password("root", "password")
    try:
        vms = await session.xenapi.VM.get_all()
        for vm in vms:
            record = await session.xenapi.VM.get_record(vm)
            print(record["name_label"])
    finally:
        await session.logout()

asyncio.run(main())

Any dotted method path under session.xenapi maps directly to the corresponding JSON-RPC call. See the XenAPI Reference for all available classes and fields.

Best Practices

1. Use get_all_records() instead of N+1 queries

The single biggest performance win. Instead of fetching a list of refs then querying each one individually, fetch everything in one call:

# SLOW — N+1 round-trips (1 for get_all + N for each get_name_label)
vms = await session.xenapi.VM.get_all()
for vm in vms:
    name = await session.xenapi.VM.get_name_label(vm)
    print(name)

# FAST — 1 round-trip, returns {ref: {field: value, ...}, ...}
records = await session.xenapi.VM.get_all_records()
for ref, rec in records.items():
    if not rec["is_a_template"] and not rec["is_a_snapshot"]:
        print(f"{rec['name_label']} ({rec['power_state']})")

This applies to every XenAPI class: host, SR, network, VM, pool, etc.

2. Use asyncio.gather() for independent calls

When you need results from multiple independent API calls, run them concurrently:

# SLOW — sequential, each awaits the previous
major = await session.xenapi.host.get_API_version_major(host)
minor = await session.xenapi.host.get_API_version_minor(host)

# FAST — concurrent, both requests in flight at the same time
major, minor = await asyncio.gather(
    session.xenapi.host.get_API_version_major(host),
    session.xenapi.host.get_API_version_minor(host),
)

You can also gather across different classes:

hosts, vms, networks = await asyncio.gather(
    session.xenapi.host.get_all_records(),
    session.xenapi.VM.get_all_records(),
    session.xenapi.network.get_all_records(),
)

3. Always clean up the session

Use try/finally to ensure logout() is called, which closes both the server-side session and the underlying HTTP connection:

session = AsyncXenAPISession("https://xen-host")
await session.login_with_password("root", "password")
try:
    # ... your code ...
finally:
    await session.logout()

Key Takeaways

Pattern Calls Approach
List objects with fields 1 get_all_records()
Multiple independent values N concurrent asyncio.gather()
Lookup one field for one ref 1 get_<field>(ref)

Run Tests

git clone git@github.com:acefei/async-xenapi.git
cd async-xenapi
cp .env.example .env  # then edit .env with your credentials
cd python
uv sync
uv run pytest tests/test_get_xapi_version.py -v

License

LGPL-2.1-only

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_xenapi-1.0.5.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_xenapi-1.0.5-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file async_xenapi-1.0.5.tar.gz.

File metadata

  • Download URL: async_xenapi-1.0.5.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for async_xenapi-1.0.5.tar.gz
Algorithm Hash digest
SHA256 d4bff7675567f88c640ddaec72173c10c5a90b3aa34548a361780247bbd5e16d
MD5 65a83be7f6ac4b277e3aefcd304fe47a
BLAKE2b-256 de05d558782e1bda1c6a23da7e0fa226904f803d5fa1480bb3ce70463e3dcfa3

See more details on using hashes here.

File details

Details for the file async_xenapi-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: async_xenapi-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for async_xenapi-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 eebcb8b5d068ab79c55da26369edbcdb26bb017b4709e2dee16937afa893027d
MD5 896002d10b702bf6e81246c5b2bd74db
BLAKE2b-256 69aea7b4e4a050c6fd283388d91b569e2e3bc64dfbd9f1f6b79c8228ae4c3dcd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page