FastUUID
FastUUID provides CPython bindings to a Rust UUID library, offering an API that is functionally identical to Python's built-in `uuid` module but with significantly improved performance. It supports UUID versions v1, v3, v4, v5, and v7. The current version is 0.14.0, and it maintains an active development status with regular releases.
Warnings
- breaking Installing `fastuuid` requires a Rust toolchain to be present on the system, as it compiles Rust code into CPython bindings. Environments without Rust (e.g., slim Docker images) will fail during `pip install`.
- gotcha FastUUID is stricter when parsing hexadecimal representations of UUIDs compared to Python's built-in `uuid` module. This means some malformed UUID strings that might have been accepted by `uuid.UUID()` previously could now raise `ValueError`.
- gotcha There are multiple projects named 'fastuuid' across different languages (e.g., Go, other Rust implementations). The Python `fastuuid` library (`fastuuid/fastuuid` on GitHub) specifically aims for API compatibility with Python's standard `uuid` module and RFC 4122 compliance, supporting v1, v3, v4, v5, and v7. Other 'fastuuid' projects, especially in Go or older Rust versions, might have different characteristics, such as generating 192-bit non-RFC compliant or 'guessable' UUIDs.
- gotcha While `fastuuid.uuid4_bulk()` explicitly releases the Global Interpreter Lock (GIL) for performance when generating many UUIDs, individual calls to functions like `fastuuid.uuid4()` are not explicitly stated to release the GIL. For highly concurrent scenarios generating single UUIDs, performance benefits might be limited if the GIL is held.
Install
-
pip install fastuuid
Imports
- UUID
from fastuuid import UUID
- uuid1
from fastuuid import uuid1
- uuid3
from fastuuid import uuid3
- uuid4
from fastuuid import uuid4
- uuid5
from fastuuid import uuid5
- uuid7
from fastuuid import uuid7
Quickstart
from fastuuid import uuid4, UUID, uuid4_bulk
# Generate a UUID v4
new_uuid = uuid4()
print(f"Generated UUID v4: {new_uuid}")
# Create a UUID from a string
existing_uuid_str = "12345678-1234-5678-1234-567812345678"
try:
parsed_uuid = UUID(existing_uuid_str)
print(f"Parsed UUID: {parsed_uuid}")
except ValueError as e:
print(f"Error parsing UUID: {e}")
# Generate multiple UUID v4s efficiently (releases GIL)
bulk_uuids = uuid4_bulk(100)
print(f"Generated {len(bulk_uuids)} UUIDs in bulk. First one: {bulk_uuids[0]}")