Skip to main content

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.

Release files for python-cq 0.25.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-cq 0.25.0
File Size Uploaded
python_cq-0.25.0.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-cq 0.25.0
File Interpreter ABI Platform
python_cq-0.25.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.3 kB

Release files / python_cq-0.25.0.tar.gz

Download URL python_cq-0.25.0.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a86f7ea6d3c6a7f4f7123c04cc98aa6823a3c04d7d9b295abae57982d93c99d5
BLAKE2b-256 checksum
How to use checksums
127b45dc03eddc730db6a490d6c8dc70695f3643fb42028437f2503c784d4211
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","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}

Release files / python_cq-0.25.0-py3-none-any.whl

Download URL python_cq-0.25.0-py3-none-any.whl
Size 23.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f49e63e91d30dd0c53f718e579f528e7d491321f21167d3302d2b80d75820c06
BLAKE2b-256 checksum
How to use checksums
71bf505a8f948a0321f327fd7280b80a88e103daab5a4f40c61463002b3a1532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","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}

Release history Release notifications | RSS feed

This release

0.25.0 This release

2 release files

0.24.0

2 release files

0.23.1

2 release files

0.23.0

2 release files

0.22.1

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.17.0

2 release files

0.16.1

2 release files

0.16.0

2 release files

0.15.2

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.1

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.3

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.4

2 release files

0.11.3

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page