Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ async def context():
await handle_event(product_event)
```

> **Note**: Async event handlers are executed in parallel using `asyncio.gather`. Sync handlers are called sequentially.

### Stories

Stories provide a pattern for defining sequential business operations with optional hooks for execution tracking,
Expand Down
9 changes: 7 additions & 2 deletions src/dddkit/dataclasses/events.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from asyncio import get_running_loop
from collections.abc import Awaitable, Callable
from dataclasses import dataclass, field
Expand Down Expand Up @@ -84,11 +85,15 @@
handler(event)

async def async_publish(self, event: DomainEvent) -> None:
for handler in self._get_subscribers(event):
handlers = self._get_subscribers(event)
async_handlers = []
for handler in handlers:
if iscoroutinefunction(handler):
await handler(event)
async_handlers.append(handler(event))

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 92 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)
else:
handler(event)
if async_handlers:
await asyncio.gather(*async_handlers)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 96 in src/dddkit/dataclasses/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

def instance(self, obj_type: type[ET] | tuple[type[ET], ...] | None) -> Callable[[HandlerEvent], HandlerEvent]:
_type = obj_type if obj_type is not None else type(None)
Expand Down
9 changes: 7 additions & 2 deletions src/dddkit/pydantic/events.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from asyncio import get_running_loop
from collections.abc import Awaitable, Callable
from datetime import datetime
Expand Down Expand Up @@ -86,11 +87,15 @@
handler(event)

async def async_publish(self, event: DomainEvent) -> None:
for handler in self._get_subscribers(event):
handlers = self._get_subscribers(event)
async_handlers = []
for handler in handlers:
if iscoroutinefunction(handler):
await handler(event)
async_handlers.append(handler(event))

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)

Check warning on line 94 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Type of "append" is partially unknown   Type of "append" is "(object: Unknown, /) -> None" (reportUnknownMemberType)
else:
handler(event)
if async_handlers:
await asyncio.gather(*async_handlers)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.14)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.10)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.13)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.12)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

Check warning on line 98 in src/dddkit/pydantic/events.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

Argument type is unknown   Argument corresponds to parameter "coros_or_futures" in function "gather" (reportUnknownArgumentType)

def instance(self, obj_type: type[ET] | tuple[type[ET], ...] | None) -> Callable[[HandlerEvent], HandlerEvent]:
_type = obj_type if obj_type is not None else type(None)
Expand Down