cosmicpython / code

Example application code for the python architecture book
Other
2.07k stars 922 forks source link

Chapter 13. Dependency Injection use same uow concurrently in entrypoint? #69

Closed wyg031113 closed 1 year ago

wyg031113 commented 1 year ago

booststrap script bind same uow to all handlers? Every request use same uow object. Should we create uow on every http request/or every event/every command ? UoW is consistent boundary, it commit changes after one handler. but now, concurrent committing concurrently will broke it?


def bootstrap(
    start_orm: bool = True,
    uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), #sameobject
    notifications: AbstractNotifications = None,
    publish: Callable = redis_eventpublisher.publish,
) -> messagebus.MessageBus:
class MessageBus:
    def __init__(
        self,
        uow: unit_of_work.AbstractUnitOfWork,
        event_handlers: Dict[Type[events.Event], List[Callable]],
        command_handlers: Dict[Type[commands.Command], Callable],
    ):
        self.uow = uow  #sameobject
        self.event_handlers = event_handlers
        self.command_handlers = command_handlers
hjwp commented 1 year ago

yes, you shouldn't use this exact code if you're working in a multithreaded environment.

hjwp commented 1 year ago

see also the discussion here! https://github.com/cosmicpython/code/issues/23