Skip to main content

CQRS library for async Python projects.

Project description

python-cq

PyPI - Version PyPI - Downloads

python-cq is an async-first Python library for organizing code around CQRS. It separates reads (queries), writes (commands), and notifications (events) into dedicated message buses, and lets you plug in any dependency injection framework behind a small protocol.

What is CQRS?

CQRS (Command Query Responsibility Segregation) splits read operations from write operations. Each operation has a single, well-defined responsibility, which:

  • clarifies intent: a CreateUserCommand does one thing, and its name says so;
  • keeps handlers small: one message, one handler, easy to test in isolation;
  • makes side effects explicit: events fan out to subscribers without coupling the producer to them.

CQRS is often discussed alongside distributed systems and Event Sourcing, but the pattern is just as useful in a local or monolithic application. The boundaries it draws are valuable on their own.

Three message types

Type Intent Handlers Returns
Command Change the state of the system Exactly one The handler's return value
Query Read state without side effects Exactly one The handler's return value
Event Notify that something has happened Zero, one, or many Nothing

A Command is allowed to return a value (for convenience, typically an id or a result object), but that does not mean it should be used as a query. Keep intent clear.

Installation

Requires Python 3.12 or higher.

With the default DI backend (python-injection, recommended):

pip install "python-cq[injection]"

Without dependency injection (you will need to implement a DIAdapter):

pip install python-cq

Quickstart

import asyncio
from cq import CommandBus, command_handler
from dataclasses import dataclass
from injection import inject

@dataclass
class CreateUserCommand:
    name: str
    email: str

@command_handler
class CreateUserHandler:
    async def handle(self, command: CreateUserCommand) -> int:
        # ... persist the user, return its id
        return 42

@inject
async def main(bus: CommandBus[int]) -> None:
    command = CreateUserCommand(name="Ada", email="ada@example.com")
    user_id = await bus.dispatch(command)
    print(f"Created user {user_id}")

asyncio.run(main())

The decorator registers the handler against the type of its first handle parameter. The bus is resolved by the DI container and dispatched to that handler.

Prerequisites

Familiarity with the following helps you get the most out of python-cq:

  • CQRS, in particular the distinction between Commands, Queries, and Events.
  • Domain Driven Design (DDD), particularly aggregates and bounded contexts, which complement CQRS well.

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

python_cq-0.18.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

python_cq-0.18.0-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file python_cq-0.18.0.tar.gz.

File metadata

  • Download URL: python_cq-0.18.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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 python_cq-0.18.0.tar.gz
Algorithm Hash digest
SHA256 ce16161c52d74b89cc23498cb6bd79f17ecf629573291ff62755b68d058695b3
MD5 446b380f4db892e2aff71c2c7e71997e
BLAKE2b-256 15c768dc1d27c71f5ee0a0cad46fcdf568ce653fdf397ff58ff7dc558e5a9862

See more details on using hashes here.

File details

Details for the file python_cq-0.18.0-py3-none-any.whl.

File metadata

  • Download URL: python_cq-0.18.0-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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 python_cq-0.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df55f194bf04812df0d5b20e5e7373c20aae0e18385ed3c8c80f22e58dfc67c1
MD5 a1de9c9d688bbc453d279c01b4dcd5f8
BLAKE2b-256 a67ff40febe883f934ea5084d2ab9267781551ed8217f8cc336c77d9b8df12be

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