cosmicpython/code

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

wyg031113 opened this issue · 2 comments

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

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

hjwp commented

see also the discussion here! #23